身份證最后一位校驗(yàn)碼計(jì)算規(guī)則:
1.將前面的身份證號(hào)碼17位數(shù)分別乘以不同的系數(shù)耕腾。從第一位到第十七位的系數(shù)分別為:7. 9 .10.5. 8. 4. 2. 1. 6. 3. 7. 9. 10. 5. 8. 4. 2.
2.將這17位數(shù)字和系數(shù)相乘的結(jié)果相加哩盲。
3.用加出來和除以11皆愉,看余數(shù)是多少?
4余數(shù)只可能有0 避乏、1胳蛮、 2、 3溉苛、 4、 5弄诲、 6愚战、 7、 8齐遵、 9寂玲、 10這11個(gè)數(shù)字。其分別對(duì)應(yīng)的最后一位身份證的號(hào)碼為1 .0. X. 9. 8. 7. 6. 5. 4. 3. 2.梗摇。
5.通過上面得知如果余數(shù)是2拓哟,就會(huì)在身份證的第18位數(shù)字上出現(xiàn)羅馬數(shù)字的Ⅹ。如果余數(shù)是10伶授,身份證的最后一位號(hào)碼就是2断序。
身份證號(hào)碼倒數(shù)第二位是用來表示性別的(奇數(shù)為男性,偶數(shù)為女性)
例如:某男性的身份證號(hào)碼是34052419800101001X流纹。我們要看看這個(gè)身份證是不是合法的身份證。
首先:我們得出违诗,前17位的乘積和是189
然后:用189除以11得出的結(jié)果是17 + 2/11漱凝,也就是說余數(shù)是2。
最后:通過對(duì)應(yīng)規(guī)則就可以知道余數(shù)2對(duì)應(yīng)的數(shù)字是x诸迟。所以茸炒,這是一個(gè)合格的身份證號(hào)碼
工具類代碼:
import android.support.v4.util.SimpleArrayMap;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* create by : Jarry Leo
* date : 2018/8/15 16:47
*/
public class IdCardValidate {
/**
* 檢查身份證號(hào)碼是否合法
*
* @param id 身份證號(hào)碼
* @return 檢查結(jié)果
*/
public static boolean check(String id) {
return checkLength(id) &&
checkNumber(id) &&
checkLastCode(id) &&
checkDate(id) &&
checkAreaCode(id);
}
//檢查長(zhǎng)度
private static boolean checkLength(String id) {
return id.length() == 18;
}
//檢查數(shù)字
private static boolean checkNumber(String id) {
String reg = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";
return id.matches(reg);
}
//檢查校驗(yàn)碼
private static boolean checkLastCode(String id) {
String[] valCodeArr = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
String[] wi = {"7", "9", "10", "5", "8", "4", "2", "1",
"6", "3", "7", "9", "10", "5", "8", "4", "2"};
int sum = 0;
for (int i = 0; i < wi.length; i++) {
sum = sum + Integer.parseInt(String.valueOf(id.charAt(i)))
* Integer.parseInt(wi[i]);
}
int modValue = sum % 11;
return valCodeArr[modValue].equals(id.substring(17));
}
//檢查地區(qū)碼
private static boolean checkAreaCode(String id) {
SimpleArrayMap<String, String> map = new SimpleArrayMap<>();
map.put("11", "北京");
map.put("12", "天津");
map.put("13", "河北");
map.put("14", "山西");
map.put("15", "內(nèi)蒙古");
map.put("21", "遼寧");
map.put("22", "吉林");
map.put("23", "黑龍江");
map.put("31", "上海");
map.put("32", "江蘇");
map.put("33", "浙江");
map.put("34", "安徽");
map.put("35", "福建");
map.put("36", "江西");
map.put("37", "山東");
map.put("41", "河南");
map.put("42", "湖北");
map.put("43", "湖南");
map.put("44", "廣東");
map.put("45", "廣西");
map.put("46", "海南");
map.put("50", "重慶");
map.put("51", "四川");
map.put("52", "貴州");
map.put("53", "云南");
map.put("54", "西藏");
map.put("61", "陜西");
map.put("62", "甘肅");
map.put("63", "青海");
map.put("64", "寧夏");
map.put("65", "新疆");
map.put("71", "臺(tái)灣");
map.put("81", "香港");
map.put("82", "澳門");
map.put("91", "國(guó)外");
return map.containsKey(id.substring(0, 2));
}
//檢查日期
private static boolean checkDate(String id) {
String strYear = id.substring(6, 10);// 年份
String strMonth = id.substring(10, 12);// 月份
String strDay = id.substring(12, 14);// 日期
String date = strYear + "-" + strMonth + "-" + strDay;
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
try {
Calendar calendar = Calendar.getInstance();
Date parse = simpleDateFormat.parse(date);
boolean after = calendar.getTime().after(parse);
calendar.add(Calendar.YEAR, -150);
boolean before = calendar.getTime().before(parse);
return after && before;
} catch (ParseException e) {
return false;
}
}
}