|
@@ -58,21 +58,26 @@ public class CalculateUtils {
|
|
|
}
|
|
|
|
|
|
public Integer calculateAge(String idNumber) {
|
|
|
- // 检查身份证号码的有效性
|
|
|
- if (idNumber == null || idNumber.length() != 18) {
|
|
|
+ try{
|
|
|
+ // 检查身份证号码的有效性
|
|
|
+ if (idNumber == null || idNumber.length() != 18) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 提取出生日期信息
|
|
|
+ String birthDateString = idNumber.substring(6, 14);
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
+ LocalDate birthDate = LocalDate.parse(birthDateString, formatter);
|
|
|
+
|
|
|
+ // 获取当前日期
|
|
|
+ LocalDate currentDate = LocalDate.now();
|
|
|
+
|
|
|
+ // 计算年龄
|
|
|
+ return Period.between(birthDate, currentDate).getYears();
|
|
|
+ } catch (Exception e) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- // 提取出生日期信息
|
|
|
- String birthDateString = idNumber.substring(6, 14);
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
- LocalDate birthDate = LocalDate.parse(birthDateString, formatter);
|
|
|
-
|
|
|
- // 获取当前日期
|
|
|
- LocalDate currentDate = LocalDate.now();
|
|
|
-
|
|
|
- // 计算年龄
|
|
|
- return Period.between(birthDate, currentDate).getYears();
|
|
|
}
|
|
|
|
|
|
|