Java中通过身份证解析出:年龄、性别、出生日期。

Java中通过身份证解析出:年龄性别出生日期。下面是一个java类


package com.thinkgem.jeesite.modules.cyry;
import com.thinkgem.jeesite.modules.wcbxx.dao.WcbxxDao;
import com.thinkgem.jeesite.modules.wcbxx.entity.Wcbxx;
import com.thinkgem.jeesite.modules.wcbxx.service.WcbxxService;


import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2016/2/25.
 */
public class IdCard {
    /** 中国公民身份证号码最小长度。 */
    public  final int CHINA_ID_MIN_LENGTH = 15;

    /** 中国公民身份证号码最大长度。 */
    public  final int CHINA_ID_MAX_LENGTH = 18;
    /**
     * 根据身份编号获取年龄
     *
     * @param idCard
     *            身份编号
     * @return 年龄
     */
    public static int getAgeByIdCard(String idCard) {
        int iAge = 0;
        Calendar cal = Calendar.getInstance();
        String year = idCard.substring(6, 10);
        int iCurrYear = cal.get(Calendar.YEAR);
        iAge = iCurrYear - Integer.valueOf(year);
        return iAge;
    }

    /**
     * 根据身份编号获取生日
     *
     * @param idCard 身份编号
     * @return 生日(yyyyMMdd)
     */
    public static String getBirthByIdCard(String idCard) {
        return idCard.substring(6, 14);
    }

    /**
     * 根据身份编号获取生日年
     *
     * @param idCard 身份编号
     * @return 生日(yyyy)
     */
    public static Short getYearByIdCard(String idCard) {
        return Short.valueOf(idCard.substring(6, 10));
    }

    /**
     * 根据身份编号获取生日月
     *
     * @param idCard
     *            身份编号
     * @return 生日(MM)
     */
    public static Short getMonthByIdCard(String idCard) {
        return Short.valueOf(idCard.substring(10, 12));
    }

    /**
     * 根据身份编号获取生日天
     *
     * @param idCard
     *            身份编号
     * @return 生日(dd)
     */
    public static Short getDateByIdCard(String idCard) {
        return Short.valueOf(idCard.substring(12, 14));
    }

    /**
     * 根据身份编号获取性别
     *
     * @param idCard 身份编号
     * @return 性别(M-F-N-未知)
     */
    public static String getGenderByIdCard(String idCard) {
        String sGender = "未知";

        String sCardNum = idCard.substring(16, 17);
        if (Integer.parseInt(sCardNum) % 2 != 0) {
            sGender = "1";//男
        } else {
            sGender = "2";//女
        }
        return sGender;
    }

//修改年龄的方法
 /*   @RequestMapping(value = "shebaoshuju",method = RequestMethod.GET)
    public String shebaoshuju(){
        List<Wcbxx> list= wcbxxservice.all();
        int  cou=0;
        for(Wcbxx w:list){
            if(w.getIdcard()!=null && w.getIdcard().length()>17) {
                String sex = IdCard.getGenderByIdCard(w.getIdcard());
                int age = IdCard.getAgeByIdCard(w.getIdcard());
                w.setAge(Long.valueOf(age));
                w.setSex(sex);
                int a = wcbxxservice.updatess(w);
                if (a>0) {
                    System.out.println("姓名" + w.getRname() + "身份证" + w.getIdcard() + "年龄:" + age + "性别:" + sex);
                }
            }
            cou++;
            System.out.println(cou);
        }
        return "";
    }*/

    @Test
    public static  void  main(String [] a){
        String idcard="460200199209275127";
        String sex= getGenderByIdCard(idcard);
        System.out.println("性别:" + sex);
        int age= getAgeByIdCard(idcard);
        System.out.println("年龄:" + age);
        Short nian=getYearByIdCard(idcard);
        Short yue=getMonthByIdCard(idcard);
        Short ri=getDateByIdCard(idcard);
        System.out.print(nian+"年"+yue+"月"+ri+"日");

        String sr=getBirthByIdCard(idcard);
        System.out.println("生日:" + sr);
    }

}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java代码,可以通过身份证号码解析年龄性别和生日: ```java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class IdCardParser { public static void main(String[] args) { String idCard = "110101199003077777"; String birthday = getBirthday(idCard); String gender = getGender(idCard); int age = getAge(idCard); System.out.println("Birthday: " + birthday); System.out.println("Gender: " + gender); System.out.println("Age: " + age); } public static String getBirthday(String idCard) { String birthday = ""; if (idCard.length() == 18) { birthday = idCard.substring(6, 14); } else if (idCard.length() == 15) { birthday = "19" + idCard.substring(6, 12); } SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); try { Date date = sdf.parse(birthday); birthday = sdf.format(date); } catch (ParseException e) { e.printStackTrace(); } return birthday; } public static String getGender(String idCard) { String gender = ""; if (idCard.length() == 18) { gender = idCard.substring(16, 17); } else if (idCard.length() == 15) { gender = idCard.substring(14, 15); } int genderNum = Integer.parseInt(gender); if (genderNum % 2 == 0) { return "Female"; } else { return "Male"; } } public static int getAge(String idCard) { int age = 0; Calendar calendar = Calendar.getInstance(); int currentYear = calendar.get(Calendar.YEAR); int birthYear = 0; if (idCard.length() == 18) { birthYear = Integer.parseInt(idCard.substring(6, 10)); } else if (idCard.length() == 15) { birthYear = Integer.parseInt("19" + idCard.substring(6, 8)); } age = currentYear - birthYear; return age; } } ``` 注意:这段代码只是一个简单的示例,实际应用需要考虑更多的情况,比如身份证号码的合法性等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值