身份证号验证工具类
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Objects;
import java.util.regex.Pattern;/*** 身份證號碼的格式:610821-20061222-612-X* 由18位數字組成:前6位為地址碼,第7至14位為出生日期碼,第15至17位為順序碼,* 第18位為校驗碼。檢驗碼分別是0-10共11個數字,當檢驗碼為“10”時,為了保證公民身份證號碼18位,所以用“X”表示。雖然校驗碼為“X”不能更換,但若需全用數字表示,只需將18位公民身份號碼轉換成15位居民身份證號碼,去掉第7至8位和最后1位3個數碼。* 當今的身份證號碼有15位和18位之分。1985年我國實行居民身份證制度,當時簽發的身份證號碼是15位的,1999年簽發的身份證由于年份的擴展(由兩位變為四位)和末尾加了效驗碼,就成了18位。* (1)前1、2位數字表示:所在省份的代碼;* (2)第3、4位數字表示:所在城市的代碼;* (3)第5、6位數字表示:所在區縣的代碼;* (4)第7~14位數字表示:出生年、月、日;* (5)第15、16位數字表示:所在地的派出所的代碼;* (6)第17位數字表示性別:奇數表示男性,偶數表示女性* (7)第18位數字是校檢碼:根據一定算法生成*/
public class IDValidator {/*** 校驗碼*/private static final String[] validateCodes = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};/*** 因子*/private static final int[] weights = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};/*** 地區編碼*/private static final Hashtable zones = new Hashtable();/*** 初始化省市編碼*/static {zones.put("11", "北京");zones.put("12", "天津");zones.put("13", "河北");zones.put("14", "山西");zones.put("15", "內蒙古");zones.put("21", "遼寧");zones.put("22", "吉林");zones.put("23", "黑龍江");zones.put("31", "上海");zones.put("32", "江蘇");zones.put("33", "浙江");zones.put("34", "安徽");zones.put("35", "福建");zones.put("36", "江西");zones.put("37", "山東");zones.put("41", "河南");zones.put("42", "湖北");zones.put("43", "湖南");zones.put("44", "廣東");zones.put("45", "廣西");zones.put("46", "海南");zones.put("50", "重慶");zones.put("51", "四川");zones.put("52", "貴州");zones.put("53", "云南");zones.put("54", "西藏");zones.put("61", "陜西");zones.put("62", "甘肅");zones.put("63", "青海");zones.put("64", "寧夏");zones.put("65", "新疆");zones.put("71", "臺灣");zones.put("81", "香港");zones.put("82", "澳門");zones.put("91", "國外");}public static void main(String[] args) {boolean result = new IDValidator().validate("130204198509042724");System.out.println(result);}/*** 身份證驗證** @param idNumber* @return*/public boolean validate(String idNumber) {if (Objects.isNull(idNumber)) {return false;}if (Pattern.matches("^[\\d]{15}$", idNumber)) {return this.validate15(idNumber);} else if (Pattern.matches("^([\\d]{17}((?i)X))|([\\d]{18})$", idNumber)) {return this.validate18(idNumber);}return false;}/*** 驗證15位身份證號** @param idNumber* @return*/private boolean validate15(String idNumber) {return this.validate18(this.convert(idNumber));}/*** 驗證18位身份證號** @param idNumber* @return*/private boolean validate18(String idNumber) {return (this.checkBirthday(idNumber) && this.checkZone(idNumber) && this.checkValidateCode(idNumber));}/*** 將15位身份證號轉成18位身份證號** @return*/private String convert(String idNumber) {String newIdNumber = idNumber.substring(0, 6) + "19" + idNumber.substring(6);// 校驗碼int sum = 0;for (int i = 0; i < 17; i++) {int ai = Integer.parseInt(String.valueOf(newIdNumber.charAt(i)));sum = sum + ai * weights[i];}int modValue = sum % 11;String validateCode;switch (modValue) {case 0:validateCode = "1";break;case 1:validateCode = "0";break;case 2:validateCode = "X";break;default:validateCode = "" + (12 - modValue);}return newIdNumber + validateCode;}/*** 驗證出生日期** @param idNumber* @return*/private boolean checkBirthday(String idNumber) {try {Date birthday = new SimpleDateFormat("yyyyMMdd").parse(idNumber.substring(6, 14));return (System.currentTimeMillis() > birthday.getTime());} catch (java.text.ParseException e) {e.printStackTrace();}return false;}/*** 驗證地區** @param idNumber* @return*/private boolean checkZone(String idNumber) {return zones.containsKey(idNumber.substring(0, 2));}/*** 驗證末位校驗碼* 判斷第18位校驗碼是否正確* 第18位校驗碼的計算方式:* 1. 對前17位數字本體碼加權求和* 公式為:S = Sum(Ai * Wi), i = 0, ... , 16* 其中Ai表示第i個位置上的身份證號碼數字值,Wi表示第i位置上的加權因子,其各位對應的值依次為: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2* 2. 用11對計算結果取模* Y = mod(S, 11)* 3. 根據模的值得到對應的校驗碼* 對應關系為:* Y值: 0 1 2 3 4 5 6 7 8 9 10* 校驗碼: 1 0 X 9 8 7 6 5 4 3 2** @param idNumber* @return*/private boolean checkValidateCode(String idNumber) {int sum = 0;for (int i = 0; i < 17; i++) {sum = sum + Integer.parseInt(String.valueOf(idNumber.charAt(i))) * weights[i];}int modValue = sum % 11;String validateCode = validateCodes[modValue];return idNumber.substring(idNumber.length() - 1).equalsIgnoreCase(validateCode);}}
總結
- 上一篇: 通达信交易接口
- 下一篇: 如何开发一个小游戏?其中有什么难点