半成品 java 身份证校验

2023-03-14,,


public static Boolean is18Card(String idCard18) {
//证件省份
HashMap<String, String> aCity = new HashMap<>();
aCity.put("11","北京");
aCity.put("12","天津");
aCity.put("13","河北");
aCity.put("14","山西");
aCity.put("15","内蒙古");
aCity.put("21","辽宁");
aCity.put("22","吉林");
aCity.put("23","黑龙江");
aCity.put("31","上海");
aCity.put("33","浙江");
aCity.put("34","安徽");

aCity.put("35","福建");
aCity.put("36","江西");
aCity.put("37","山东");
aCity.put("41","河南");
aCity.put("42","湖北");
aCity.put("43","湖南");
aCity.put("44","广东");
aCity.put("45","广西");
aCity.put("46","海南");
/* 50: "重庆",
51: "四川",
52: "贵州",
53: "云南",
54: "西藏",
61: "陕西",
62: "甘肃",
63: "青海",
64: "宁夏",
65: "新疆",
71: "台湾",
81: "香港",
82: "澳门",
91: "国外"*/

//出生日期
String year = idCard18.substring(6,10);
String month = idCard18.substring(10,12);
String day = idCard18.substring(12,14);
Date temp_date = new Date(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day) - 1);
// String temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));

//最后一位校验
int sum = 0;
int[] weights = new int[17];
weights[0]=7;
weights[1]=9;
weights[2]=10;
weights[3]=5;
weights[4]=8;
weights[5]=4;
weights[6]=2;
weights[7]=1;
weights[8]=6;
weights[9]=3;
weights[10]=7;
weights[11]=9;
weights[12]=10;
weights[13]=5;
weights[14]=8;
weights[15]=4;
weights[16]=2;
String codes = "10X98765432";
String[] code = codes.split("");
String[] idCard18Split = idCard18.split("");
for (int i = 0; i < idCard18Split.length - 1; i++) {

sum += Integer.parseInt(idCard18Split[i]) * weights[i];
}
String last = code[sum % 11]; //计算出来的最后一位证件号码

String province = idCard18.substring(0, 2);
//校验前2位省份
if(aCity.get(idCard18.substring(0, 2))==null) {
System.out.println("证件省份代码错误");
}
//出生日期验证 暂未完成
else if(temp_date.getYear()!=parseFloat(year)
||temp_date.getMonth()!=parseFloat(month)-1
||temp_date.getDate()!=parseFloat(day) )
{
System.out.println("'证件出生日期错误");
}
//证件最后一位校验码验证
else if(idCard18Split[idCard18Split.length - 1] != last) {
System.out.println("'证件出生日期错误");
}
return null;
}

半成品 java 身份证校验的相关教程结束。

《半成品 java 身份证校验.doc》

下载本文的Word格式文档,以方便收藏与打印。