public Integer checkExpertAge(String idCard) {
// 1. 获取身份证中的出生年、月、日
Integer personYear = Integer.parseInt(idCard.substring(6, 10));
Integer personMonth = Integer.parseInt(idCard.substring(10, 12));
Integer personDay = Integer.parseInt(idCard.substring(12, 14));
// 2. 获取当前时间的年、月、日
Calendar cal = Calendar.getInstance();
Integer yearNow = cal.get(Calendar.YEAR);
Integer monthNow = cal.get(Calendar.MONTH) + 1;
Integer dayNow = cal.get(Calendar.DATE);
// 3. 用当前年月日减去生日年月日
Integer yearMinus = yearNow - personYear;
Integer monthMinus = monthNow - personMonth;
Integer dayMinus = dayNow - personDay;
// 4. 周岁判断
Integer age = yearMinus;
if (yearMinus == 0) {
// 出生年份为当前年份
age = 0;
} else {
// 出生年份大于当前年份
if (monthMinus < 0) {
// 出生月份小于当前月份时,还没满周岁
age = age - 1;
}
if (monthMinus == 0) {
// 当前月份为出生月份时,判断日期
if (dayMinus < 0) {
// 出生日期小于当前月份时,没满周岁
age = age - 1;
}
}
}return age;
}
知识兔