98 lines
2.4 KiB
Java
98 lines
2.4 KiB
Java
package ink.wgink.util;
|
|
|
|
import ink.wgink.exceptions.ParamsException;
|
|
import org.joda.time.DateTime;
|
|
|
|
/**
|
|
* @ClassName: IdCardUtil
|
|
* @Description: 身份证工具
|
|
* @Author: wanggeng
|
|
* @Date: 2022/5/20 22:39
|
|
* @Version: 1.0
|
|
*/
|
|
public class IdCardUtil {
|
|
|
|
/**
|
|
* 获得生日
|
|
*
|
|
* @param idCard
|
|
* @return
|
|
*/
|
|
public static String getBirth(String idCard) {
|
|
if (!RegexUtil.isIdentity(idCard)) {
|
|
throw new ParamsException("身份证格式错误");
|
|
}
|
|
return idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-" + idCard.substring(12, 14);
|
|
}
|
|
|
|
/**
|
|
* 获取性别
|
|
*
|
|
* @param idCard
|
|
* @return
|
|
*/
|
|
public static Integer getSexNumber(String idCard) {
|
|
if (!RegexUtil.isIdentity(idCard)) {
|
|
throw new ParamsException("身份证格式错误");
|
|
}
|
|
return Integer.parseInt(idCard.substring(16, 17));
|
|
}
|
|
|
|
/**
|
|
* 获取性别
|
|
*
|
|
* @param idCard
|
|
* @return
|
|
*/
|
|
public static String getSexName(String idCard) {
|
|
int sexNumber = getSexNumber(idCard);
|
|
if (sexNumber % 2 == 0) {
|
|
return "女";
|
|
} else {
|
|
return "男";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获得年龄
|
|
*
|
|
* @param idCard
|
|
* @return
|
|
*/
|
|
public static Integer getAge(String idCard) {
|
|
int birthYear = Integer.parseInt(idCard.substring(6, 10));
|
|
int birthMonth = Integer.parseInt(idCard.substring(10, 12));
|
|
int birthDay = Integer.parseInt(idCard.substring(12, 14));
|
|
|
|
DateTime now = DateTime.now();
|
|
int year = now.getYear();
|
|
int month = now.getMonthOfYear();
|
|
int day = now.getDayOfMonth();
|
|
|
|
int age = year - birthYear;
|
|
if (month < birthMonth) {
|
|
return age - 1;
|
|
} else if (month == birthMonth && day < birthDay) {
|
|
return age - 1;
|
|
}
|
|
return age;
|
|
}
|
|
|
|
/**
|
|
* 身份证脱敏
|
|
*
|
|
* @param idCard
|
|
* @return
|
|
*/
|
|
public static String removeSensitive(String idCard) {
|
|
if (!RegexUtil.isIdentity(idCard)) {
|
|
throw new ParamsException("身份证格式错误");
|
|
}
|
|
if (idCard.length() == 15) {
|
|
return idCard.replaceAll("(\\w{6})\\w*(\\w{3})", "$1******$2");
|
|
}
|
|
return idCard.replaceAll("(\\w{6})\\w*(\\w{3})", "$1*********$2");
|
|
}
|
|
|
|
}
|