| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package cn.com.goldenwater.util.common;
- import cn.com.goldenwater.dcproj.utils.StringUtils;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.regex.Pattern;
- /**
- * IdCardUtils
- * 从身份证获取到出生年月 和 年龄
- *
- * @author lxf
- * @version 1.0
- * @date 2023/01/17 14:55
- **/
- public class IdCardUtils {
- /**
- * 15位身份证号
- * 1980年以前的身份证
- */
- private static final Integer FIFTEEN_ID_CARD=15;
- /**
- * 18位身份证号
- */
- private static final Integer EIGHTEEN_ID_CARD=18;
- /**
- * 出生日期格式化
- */
- private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- /**
- * 校验 15位到18位的身份证号
- */
- public static boolean checkIdCard(String idCard) {
- String regex = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
- return Pattern.matches(regex, idCard);
- }
- /**
- * 获取到出生日期 yyyy年mm月dd日
- * @param IDCard
- * @return
- */
- public static String getBirthday(String IDCard){
- String birthday="";
- String year="";
- String month="";
- String day="";
- if (StringUtils.isNotBlank(IDCard)){
- //15位身份证号
- if (IDCard.length() == FIFTEEN_ID_CARD){
- // 身份证上的年份(15位身份证为1980年前的)
- year = "19" + IDCard.substring(6, 8);
- //身份证上的月份
- month = IDCard.substring(8, 10);
- //身份证上的日期
- day= IDCard.substring(10, 12);
- //18位身份证号
- }else if(IDCard.length() == EIGHTEEN_ID_CARD){
- // 身份证上的年份
- year = IDCard.substring(6).substring(0, 4);
- // 身份证上的月份
- month = IDCard.substring(10).substring(0, 2);
- //身份证上的日期
- day=IDCard.substring(12).substring(0,2);
- }
- birthday=year+"年"+month+"月"+day+"日";
- }
- return birthday;
- }
- public static Date getBirthdayDate(String IDCard){
- String bornStr = getBirthday(IDCard);
- if(StringUtils.isBlank(bornStr)){
- return null;
- }
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
- try {
- Date bornDate = sdf.parse(bornStr);
- return bornDate;
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 获取年龄
- * @param IDCard
- * @return
- */
- public static Integer getAge(String IDCard){
- Integer age = 0;
- Date date = new Date();
- if (StringUtils.isNotBlank(IDCard)&& checkIdCard(IDCard)){
- // 当前年份
- String fyear = format.format(date).substring(0, 4);
- // 当前月份
- String fyue = format.format(date).substring(5, 7);
- //15位身份证号
- if (IDCard.length() == FIFTEEN_ID_CARD){
- // 身份证上的年份(15位身份证为1980年前的)
- String uyear = "19" + IDCard.substring(6, 8);
- // 身份证上的月份
- String uyue = IDCard.substring(8, 10);
- if (Integer.parseInt(uyue) <= Integer.parseInt(fyue)) {
- age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1;
- // 当前用户还没过生
- } else {
- age = Integer.parseInt(fyear) - Integer.parseInt(uyear);
- }
- //18位身份证号
- }else if(IDCard.length() == EIGHTEEN_ID_CARD){
- // 身份证上的年份
- String year = IDCard.substring(6).substring(0, 4);
- // 身份证上的月份
- String yue = IDCard.substring(10).substring(0, 2);
- // 当前月份大于用户出身的月份表示已过生日
- if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
- age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
- // 当前用户还没过生日
- } else {
- age = Integer.parseInt(fyear) - Integer.parseInt(year);
- }
- }
- }
- return age;
- }
- /**
- * 获取到性别
- * @param IDCard
- * @return
- */
- public static String getSex(String IDCard){
- String sex ="";
- if (StringUtils.isNotBlank(IDCard)){
- //15位身份证号
- if (IDCard.length() == FIFTEEN_ID_CARD){
- if (Integer.parseInt(IDCard.substring(14, 15)) % 2 == 0) {
- sex = "女";
- } else {
- sex = "男";
- }
- //18位身份证号
- }else if(IDCard.length() == EIGHTEEN_ID_CARD){
- // 判断性别
- if (Integer.parseInt(IDCard.substring(16).substring(0, 1)) % 2 == 0) {
- sex = "女";
- } else {
- sex = "男";
- }
- }
- }
- return sex;
- }
- }
|