引入工具类hutool
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.11</version>
</dependency>
实例
package org.jeecg.modules.zzyhutool;
import cn.hutool.core.util.IdcardUtil;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@Slf4j
public class IdcardUtilTest {
public static void main(String[] args) {
String idCard = "350832200808080048";
boolean validCard = IdcardUtil.isValidCard(idCard);
log.info("验证身份证是否合法:{}", validCard);
String birthByIdCard = IdcardUtil.getBirthByIdCard(idCard);
log.info("获取生日:{}", birthByIdCard);
int ageByIdCard = IdcardUtil.getAgeByIdCard(idCard);
log.info("获取年龄:{}", ageByIdCard);
LocalDateTime localDateTime = LocalDateTime.parse("2018-03-13 14:41:14", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = localDateTime.atZone(zoneId);
Date date = Date.from(zdt.toInstant());
int ageByIdCard1 = IdcardUtil.getAgeByIdCard(idCard, date);
log.info("获取年龄:{}", ageByIdCard1);
Short yearByIdCard = IdcardUtil.getYearByIdCard(idCard);
log.info("获取生日年:{}", yearByIdCard);
Short monthByIdCard = IdcardUtil.getMonthByIdCard(idCard);
log.info("获取生日月:{}", monthByIdCard);
Short dayByIdCard = IdcardUtil.getDayByIdCard(idCard);
log.info("获取生日天:{}", dayByIdCard);
int genderByIdCard = IdcardUtil.getGenderByIdCard(idCard);
log.info("获取性别:{}", genderByIdCard);
String provinceByIdCard = IdcardUtil.getProvinceByIdCard(idCard);
log.info("获取省份:{}", provinceByIdCard);
}
}