btxtgxq-system-population/src/main/java/com/cm/population/utils/IdCardVerifyUtil.java
2023-12-11 17:15:11 +08:00

263 lines
8.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.cm.population.utils;
import com.cm.common.utils.DateUtil;
import com.cm.common.utils.UUIDUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 15、18位身份证号验证工具类
* @author renpc
*/
public class IdCardVerifyUtil {
final static Map<Integer, String> zoneNum = new HashMap<Integer, String>();
static {
zoneNum.put(11, "北京");
zoneNum.put(12, "天津");
zoneNum.put(13, "河北");
zoneNum.put(14, "山西");
zoneNum.put(15, "内蒙古");
zoneNum.put(21, "辽宁");
zoneNum.put(22, "吉林");
zoneNum.put(23, "黑龙江");
zoneNum.put(31, "上海");
zoneNum.put(32, "江苏");
zoneNum.put(33, "浙江");
zoneNum.put(34, "安徽");
zoneNum.put(35, "福建");
zoneNum.put(36, "江西");
zoneNum.put(37, "山东");
zoneNum.put(41, "河南");
zoneNum.put(42, "湖北");
zoneNum.put(43, "湖南");
zoneNum.put(44, "广东");
zoneNum.put(45, "广西");
zoneNum.put(46, "海南");
zoneNum.put(50, "重庆");
zoneNum.put(51, "四川");
zoneNum.put(52, "贵州");
zoneNum.put(53, "云南");
zoneNum.put(54, "西藏");
zoneNum.put(61, "陕西");
zoneNum.put(62, "甘肃");
zoneNum.put(63, "青海");
zoneNum.put(64, "宁夏");
zoneNum.put(65, "新疆");
zoneNum.put(71, "台湾");
zoneNum.put(81, "香港");
zoneNum.put(82, "澳门");
zoneNum.put(91, "外国");
}
final static int[] PARITYBIT = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
final static int[] POWER_LIST = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
public IdCardVerifyUtil() {
}
public static String desensitizeBirthday(String birthday) {
if (birthday.length() < 8) {
return birthday;
}
StringBuilder sb = new StringBuilder();
sb.append(birthday.substring(0, 3));
sb.append("*");
sb.append("-");
sb.append("*");
sb.append("*");
sb.append("-");
sb.append(birthday.substring(birthday.length() - 2));
return sb.toString();
}
/**
* 身份证号码脱敏
* @param idCardNumber
* @return
*/
public static String desensitizeIdCardNumber(String idCardNumber) {
if (idCardNumber.length() < 15 || idCardNumber.length() > 18) {
return idCardNumber;
}
StringBuilder sb = new StringBuilder();
sb.append(idCardNumber.substring(0, 6));
if (idCardNumber.length() == 15) {
for (int i = 0; i < idCardNumber.length() - 8; i++) {
sb.append("*");
}
sb.append(idCardNumber.substring(idCardNumber.length() - 3));
}else {
for (int i = 0; i < idCardNumber.length() - 10; i++) {
sb.append("*");
}
sb.append(idCardNumber.substring(idCardNumber.length() - 4));
}
return sb.toString();
}
/**
* 身份证验证
*@param certNo 号码内容
*@return 是否有效 null和"" 都是false
*/
public static boolean isIDCard(String certNo){
if(certNo == null || (certNo.length() != 15 && certNo.length() != 18)){
return false;
}
final char[] cs = certNo.toUpperCase().toCharArray();
//校验位数
int power = 0;
for(int i=0; i<cs.length; i++){
if(i==cs.length-1 && cs[i] == 'X'){
break;//最后一位可以 是X或x
}
if(cs[i]<'0' || cs[i]>'9'){
return false;
}
if(i < cs.length -1){
power += (cs[i] - '0') * POWER_LIST[i];
}
}
//校验区位码
if(!zoneNum.containsKey(Integer.valueOf(certNo.substring(0,2)))){
return false;
}
//校验年份
String year = null;
year = certNo.length() == 15 ? getIdCardCalendar(certNo):certNo.substring(6, 10);
final int iyear = Integer.parseInt(year);
if(iyear < 1900 || iyear > Calendar.getInstance().get(Calendar.YEAR)){
//1900年的PASS超过今年的PASS
return false;
}
//校验月份
String month = certNo.length() == 15 ? certNo.substring(8, 10) : certNo.substring(10,12);
final int imonth = Integer.parseInt(month);
if(imonth <1 || imonth >12){
return false;
}
//校验天数
String day = certNo.length() ==15 ? certNo.substring(10, 12) : certNo.substring(12, 14);
final int iday = Integer.parseInt(day);
if(iday < 1 || iday > 31){
return false;
}
//校验"校验码"
if(certNo.length() == 15){
return true;
}
return cs[cs.length -1 ] == PARITYBIT[power % 11];
}
private static String getIdCardCalendar(String certNo){
// 获取出生年月日
String birthday = certNo.substring(6, 12);
SimpleDateFormat ft = new SimpleDateFormat("yyMMdd");
Date birthdate = null;
try {
birthdate = ft.parse(birthday);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
Calendar cDay = Calendar.getInstance();
cDay.setTime(birthdate);
String year = String.valueOf(cDay.get(Calendar.YEAR));
return year;
}
/**
* 获取身份证号中的出生日期
* @param cardNo 身份证号
* @return xxxx-xx-xx
*/
public static String getIdCardBirthday(String cardNo) {
try {
if(cardNo == null || cardNo.length() == 0){
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
if(cardNo.length() == 15){
String birthday = "19" + cardNo.substring(6,12);
Date date = sdf.parse(birthday);
sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
if(cardNo.length() == 18){
String birthday = cardNo.substring(6,14);
Date date = sdf.parse(birthday);
sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
} catch (Exception e){}
return "";
}
/**
* 获取身份证号中的性别
* @param cardNo
* @return -1 无法获取 1男性0女性
*/
public static String getIdCardGender(String cardNo){
try {
if(cardNo == null || cardNo.length() == 0){
return "-1";
}
if(cardNo.length() == 15){
String genderCode = cardNo.substring(cardNo.length() - 1);
int res = Integer.parseInt(genderCode);
if(res % 2 == 0){
return "0";
} else {
return "1";
}
}
if(cardNo.length() == 18){
String genderCode = cardNo.substring(cardNo.length() - 2, cardNo.length() - 1);
int res = Integer.parseInt(genderCode);
if(res % 2 == 0){
return "0";
} else {
return "1";
}
}
} catch (Exception e){}
return "-1";
}
/**
* 根据身份证号获取年龄
* @param cardNo
* @return 如果身份证不合法无法获取返回-1
*/
public static int getIdCardAge(String cardNo){
if(!isIDCard(cardNo)){
return -1;
}
String year = cardNo.length() == 15 ? getIdCardCalendar(cardNo) : cardNo.substring(6, 10);
Integer bornYear = Integer.parseInt(year);
Integer nowYear = Integer.parseInt(DateUtil.getYear());
return nowYear - bornYear;
}
public static void main(String[] args) {
//boolean isTrue = isIDCard("150203680419303");
boolean isTrue = isIDCard("150203680419303");
System.out.println(getIdCardAge("150203680419303"));
}
}