From 5bd15bb4bb89fb3cb99d1a61483bd2d61b2f9675 Mon Sep 17 00:00:00 2001 From: wangxianzhang Date: Thu, 8 Sep 2022 16:04:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=81=E4=BB=B6=E5=8F=B7=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB=E5=A2=9E=E5=8A=A0=E5=B9=B4=E9=BE=84=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/tools/utils/IdCardRegexUtils.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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); + } }