身份证校验工具类
直接上工具类方法了,方法是参考网上其它同学所写,在此只是作为一个记录
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 身份证校验类型
*/
public class IdCardNoUtils {
// 新身份证长度18
private static final int IDCARDNO_LENGTH_NEW = 18;
// 老身份证长度15
private static final int IDCARDNO_LENGTH_OLD = 15;
private static final int IDCARDNO_SUB_LENGTH = 17;
private static final int MAX_YEAR = 100;
private static final int MAX_MONTH = 12;
private static final int MAX_DAY = 31;
private static final String CHAR = "-";
//身份证前1位每位加权因子
private static final String[] CHECK_CODE = { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2" };
//身份证第18位校检码
private static final String[] REFNUMBER = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
private static final String STRNUM= "[0-9]*";
/**
* 地区代码
* @return Hashtable
*/
private static Hashtable GetAreaCode() {
Hashtable<String,String> hashtable = new Hashtable<String,String>();
hashtable.put("11", "北京");
hashtable.put("12", "天津");
hashtable.put("13", "河北");
hashtable.put("14", "山西");
hashtable.put("15", "内蒙古");
hashtable.put("21", "辽宁");
hashtable.put("22", "吉林");
hashtable.put("23", "黑龙江");
hashtable.put("31", "上海");
hashtable.put("32", "江苏");
hashtable.put("33", "浙江");
hashtable.put("34", "安徽");
hashtable.put("35", "福建");
hashtable.put("36", "江西");
hashtable.put("37", "山东");
hashtable.put("41", "河南");
hashtable.put("42", "湖北");
hashtable.put("43", "湖南");
hashtable.put("44", "广东");
hashtable.put("45", "广西");
hashtable.put("46", "海南");
hashtable.put("50", "重庆");
hashtable.put("51", "四川");
hashtable.put("52", "贵州");
hashtable.put("53", "云南");
hashtable.put("54", "西藏");
hashtable.put("61", "陕西");
hashtable.put("62", "甘肃");
hashtable.put("63", "青海");
hashtable.put("64", "宁夏");
hashtable.put("65", "新疆");
hashtable.put("71", "台湾");
hashtable.put("81", "香港");
hashtable.put("82", "澳门");
hashtable.put("91", "国外");
return hashtable;
}
/**
*身份证验证
* @param idStr
* @return 校验信息,correct为成功,失败会返回对应的失败原因
*/
public static boolean checkIdCardNo(String idStr){
String iDCardNo = "";
try {
//判断号码的长度 15位或18位
if (idStr.length() != IDCARDNO_LENGTH_OLD && idStr.length() != IDCARDNO_LENGTH_NEW) {
// "身份证号码长度应该为15位或18位"
return false;
}
if (idStr.length() == IDCARDNO_LENGTH_NEW) {
iDCardNo = idStr.substring(0, 17);
} else if (idStr.length() == IDCARDNO_LENGTH_OLD) {
iDCardNo = idStr.substring(0, 6) + "19" + idStr.substring(6, 15);
}
if (isStrNum(iDCardNo) == false) {
// "身份证15位号码都应为数字;18位号码除最后一位外,都应为数字"
return false;
}
//判断地区码
Hashtable h = GetAreaCode();
if (h.get(iDCardNo.substring(0, 2)) == null) {
// "身份证地区编码错误"
return false;
}
//判断出生年月
String strYear = iDCardNo.substring(6, 10);
String strMonth = iDCardNo.substring(10, 12);
String strDay = iDCardNo.substring(12, 14);
if (isStrDate(strYear + strMonth + strDay) == false) {
// "身份证生日无效"
return false;
}
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > MAX_YEAR || (gc.getTime().getTime() - s.parse(strYear + CHAR + strMonth + CHAR + strDay).getTime()) < 0) {
// "身份证生日不在有效范围"
return false;
}
if (Integer.parseInt(strMonth) > MAX_MONTH || Integer.parseInt(strMonth) == 0) {
// "身份证月份无效"
return false;
}
if (Integer.parseInt(strDay) > MAX_DAY || Integer.parseInt(strDay) == 0) {
// "身份证日期无效"
return false;
}
//判断最后一位
int theLastOne = 0;
for (int i = 0; i < IDCARDNO_SUB_LENGTH; i++) {
theLastOne = theLastOne + Integer.parseInt(String.valueOf(iDCardNo.charAt(i))) * Integer.parseInt(CHECK_CODE[i]);
}
int modValue = theLastOne % 11;
String strVerifyCode = REFNUMBER[modValue];
iDCardNo = iDCardNo + strVerifyCode;
if (idStr.length() == IDCARDNO_LENGTH_NEW && !iDCardNo.equals(idStr)) {
// "身份证无效,不是合法的身份证号码"
return false;
}
return true;
}catch (Exception e){
e.printStackTrace();
}
return false;
}
/**
* 判断字符串是否为数字
* @param str
* @return
*/
private static boolean isStrNum(String str) {
Pattern pattern = Pattern.compile(STRNUM);
Matcher isNum = pattern.matcher(str);
if (isNum.matches()) {
return true;
} else {
return false;
}
}
/**
* 判断日期是否有效
* @param strDate
* @return
*/
public static boolean isStrDate(String strDate) {
if (strDate == null){
return false;
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
if (strDate.trim().length() != dateFormat.toPattern().length()){
return false;
}
//执行严格的日期匹配
dateFormat.setLenient(false);
try {
dateFormat.parse(strDate.trim());
} catch (Exception e) {
return false;
}
return true;
}
public static void main(String[] args) {
String [] cardId = {"445102198904312332",
"131100200102290696",
"51042119710630173X",
"340823196806263613",
"340703197410050615",
"440982195201230696",
"130203196110239132",
"321101197411218776",
"340501196812265178",
"330424197204277791",
"61040219710929439X",
"230826195610286476",
"350305196501252819",
"450881197212026374",
"220282197010121032",
"341723197209084377",
"451024197211265090",
"330727195609144416",
"411626197504219395",
"230605196203286219",
"21068119520426399X",
"511821195404264774",
"622923197107299798",
"622923197107299",
};
for(int i=0;i<cardId.length;i++){
System.out.println(checkIdCardNo(cardId[i]));
}
}
}```