偶然看到根据身份证获取周岁的代码,没具体考察正确与否,不清楚是否判断身份证不同位数的问题,先保留一下。
/** * 根据身份证号获取年龄(周岁) * @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; }