其他分享
首页 > 其他分享> > 通过身份证号获取年龄

通过身份证号获取年龄

作者:互联网

@RequestMapping("getAge")
    public Integer getAge(String idCard) {
        int selectYear = Integer.parseInt(idCard.substring(6, 10));
        int selectMonth = Integer.parseInt(idCard.substring(10, 12));
        int selectDay = Integer.parseInt(idCard.substring(12, 14));
        // 得到当前时间的年、月、日
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayNow = cal.get(Calendar.DATE);
        // 用当前年月日减去生日年月日
        int yearMinus = yearNow - selectYear;
        int monthMinus = monthNow - selectMonth;
        int dayMinus = dayNow - selectDay;
        //年龄
        int age = yearMinus;
        if (yearMinus > 0) {
            if (monthMinus <= 0 && dayMinus < 0) {
                age = age - 1;
            }
        } else {
            age = 0;
        }
        return age;
    }

标签:int,idCard,身份证号,substring,获取,cal,Integer,年龄,Calendar
来源: https://www.cnblogs.com/flystudy/p/15688838.html