java根据身份证号码计算年龄的方法(精确到日)
/** * 根据身份证号码计算年龄 * * @param psptNo * @return */ public int getAgeByPsptNo(String psptNo) { if (StringUtils.isEmpty(psptNo)) { return 0; } String birthDay = psptNo.substring(6, 14); String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); String yearStr = time.split("-")[0]; String monthStr = time.split("-")[1]; String dayStr = time.split("-")[2]; String yearBirthStr = birthDay.substring(0, 4); String monthBirthStr = birthDay.substring(4, 6); String dayBirthStr = birthDay.substring(6); int year = Integer.valueOf(yearStr); int yearBirth = Integer.valueOf(yearBirthStr); if (year - yearBirth <= 0) { return 0; } int age = year - yearBirth; int month = Integer.valueOf(monthStr); int monthBirth = Integer.valueOf(monthBirthStr); if (month - monthBirth > 0) { return age; } if (month - monthBirth < 0) { return --age; } int day = Integer.valueOf(dayStr); int dayBirth = Integer.valueOf(dayBirthStr); if (day - dayBirth >= 0) { return age; } return --age; }