diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IdCardRegexUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IdCardRegexUtils.java index 96d7d02a62..ff1f0b0549 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IdCardRegexUtils.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IdCardRegexUtils.java @@ -1,10 +1,15 @@ package com.epmet.commons.tools.utils; import com.epmet.commons.tools.enums.IdCardTypeEnum; +import com.epmet.commons.tools.exception.EpmetException; +import com.epmet.commons.tools.exception.ExceptionUtils; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import java.time.DateTimeException; +import java.time.LocalDate; +import java.time.Period; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -50,6 +55,7 @@ public class IdCardRegexUtils { private String birthdayMonth; private String birthdayDay; private String sex; + private Integer age; } /** @@ -116,7 +122,17 @@ public class IdCardRegexUtils { String month = matcher.group("month"); String day = matcher.group("day"); String sex = matcher.group("sex"); - return new ParsedContent(year, month, day, sex); + + // ------- 年龄Start---------- + Integer age; + try { + LocalDate birthday = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day)); + age = Period.between(birthday, LocalDate.now()).getYears(); + } catch (DateTimeException e) { + throw new EpmetException("身份证号解析年龄失败:" + ExceptionUtils.getErrorStackTrace(e)); + } + // ------- 年龄End---------- + return new ParsedContent(year, month, day, sex, age); } // 其他类型暂时不可解析 @@ -130,4 +146,10 @@ public class IdCardRegexUtils { public IdCardTypeEnum getTypeEnum() { return idCardType; } + + public static void main(String[] args) { + IdCardRegexUtils parse = IdCardRegexUtils.parse("370282198801303017"); + ParsedContent parsedResult = parse.getParsedResult(); + System.out.println(parsedResult); + } }