Browse Source

修改身份证正则解析工具类,增加出生日期[LocalDate类型]的返回

dev
wxz 2 years ago
parent
commit
1b7c69afc2
  1. 37
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/DateUtils.java
  2. 7
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IdCardRegexUtils.java

37
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/DateUtils.java

@ -22,6 +22,8 @@ import org.joda.time.format.DateTimeFormatter;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.*; import java.util.*;
@ -1112,4 +1114,39 @@ public class DateUtils {
cal.setTime(date); cal.setTime(date);
return str.contains(String.valueOf(cal.get(Calendar.DAY_OF_WEEK))); return str.contains(String.valueOf(cal.get(Calendar.DAY_OF_WEEK)));
} }
/**
* @description: java.time.LocalDate转Date
* @param localDate:
* @return
* @author: WangXianZhang
* @date: 2023/5/4 3:00 PM
*/
public static Date localDate2Date(java.time.LocalDate localDate) {
Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
return Date.from(instant);
}
/**
* @description: java.time.LocalDateTime转Date
* @param localDate:
* @return
* @author: WangXianZhang
* @date: 2023/5/4 3:00 PM
*/
public static Date localDateTime2Date(java.time.LocalDateTime localDate) {
Instant instant = localDate.atZone(ZoneId.systemDefault()).toInstant();
return Date.from(instant);
}
/**
* @description: date转化为DateTime
* @param date:
* @return
* @author: WangXianZhang
* @date: 2023/5/4 3:04 PM
*/
public static java.time.LocalDateTime date2LocalDateTime(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
} }

7
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IdCardRegexUtils.java

@ -10,6 +10,7 @@ import lombok.NoArgsConstructor;
import java.time.DateTimeException; import java.time.DateTimeException;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.Period; import java.time.Period;
import java.util.Date;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -56,6 +57,7 @@ public class IdCardRegexUtils {
private String birthdayDay; private String birthdayDay;
private String sex; private String sex;
private Integer age; private Integer age;
private LocalDate birthday;
} }
/** /**
@ -122,17 +124,18 @@ public class IdCardRegexUtils {
String month = matcher.group("month"); String month = matcher.group("month");
String day = matcher.group("day"); String day = matcher.group("day");
String sex = matcher.group("sex"); String sex = matcher.group("sex");
LocalDate birthday;
// ------- 年龄Start---------- // ------- 年龄Start----------
Integer age; Integer age;
try { try {
LocalDate birthday = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day)); birthday = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
age = Period.between(birthday, LocalDate.now()).getYears(); age = Period.between(birthday, LocalDate.now()).getYears();
} catch (DateTimeException e) { } catch (DateTimeException e) {
throw new EpmetException("身份证号解析年龄失败:" + ExceptionUtils.getErrorStackTrace(e)); throw new EpmetException("身份证号解析年龄失败:" + ExceptionUtils.getErrorStackTrace(e));
} }
// ------- 年龄End---------- // ------- 年龄End----------
return new ParsedContent(year, month, day, sex, age); return new ParsedContent(year, month, day, sex, age, birthday);
} }
// 其他类型暂时不可解析 // 其他类型暂时不可解析

Loading…
Cancel
Save