当前位置 : 主页 > 编程语言 > java >

java 根据身份证获取周岁

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt /** * 根据身份证号获取年龄(周岁) * @param idCard 身份证 * @return 年龄 */ public static int getFullAgeByIdCard(String idCard){ Calendar cal1 = Calendar.getInstance(); Calendar today = Calendar.getInstance(); c
gistfile1.txt
/**
     * 根据身份证号获取年龄(周岁)
     * @param idCard 身份证
     * @return 年龄
     */
    public static int getFullAgeByIdCard(String idCard){
        Calendar cal1 = Calendar.getInstance();
        Calendar today = Calendar.getInstance();
        cal1.set(Integer.parseInt(idCard.substring(6,10)),
                Integer.parseInt(idCard.substring(10,12)),
                Integer.parseInt(idCard.substring(12,14)));
        int m = (today.get(today.MONTH))+1 - (cal1.get(cal1.MONTH));
        int y = (today.get(today.YEAR)) - (cal1.get(cal1.YEAR));
        int d = (today.get(today.DAY_OF_MONTH)) - (cal1.get(cal1.DAY_OF_MONTH));
        if (m <= 0){
            if (m == 0){
                if (d < 0) y--;
            }else {
                y--;
            }
        }
        return y;
    }
网友评论