public class IdentityCardUtil { private static Pattern pattern = Pattern.compile("^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$"); /** * 解析身份证信息 * @param ID 身份证号 * @return */ public static IdentityCard parseIdentityInfo(String ID) { Matcher matcher = pattern.matcher(ID); if (!matcher.matches()) { throw new IllegalArgumentException("身份证号码不合法"); } String birthdayStr = ID.substring(6, 14); LocalDate birthday = LocalDate.from(DateTimeFormatter.ofPattern("yyyyMMdd").parse(birthdayStr)); long age = ChronoUnit.YEARS.between(birthday, LocalDate.now()); IdentityCard identityCard = new IdentityCard(); identityCard.setID(ID); identityCard.setAge(age); identityCard.setBirthday(birthdayStr); identityCard.setProvinceCode(ID.substring(0,2)); if (Integer.parseInt(ID.substring(16).substring(0, 1)) % 2 == 0) { identityCard.setSex(IdentityCard.Sex.FEMALE); } else { identityCard.setSex(IdentityCard.Sex.MALE); } return identityCard; } }
java读取身份证信息
最新推荐文章于 2024-07-26 22:39:31 发布