java基础-对15位或18位身份证号码的验证
生活随笔
收集整理的這篇文章主要介紹了
java基础-对15位或18位身份证号码的验证
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一.簡介
導入或添加人員信息時,后端需要對身份者號碼進行驗證,以及獲取年齡,性別,出生日期等操作,此工具類可對15位和18位身份證號碼進行驗證.
來源:https://www.cnblogs.com/Big-Boss/p/14177520.html
二.工具類
package com.agriculture.business.util;import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern;/**** 身份證校驗工具類*/ public class IDCardUtil {/*校驗規則:如果為15位,只能是15位數字;前兩位滿足省/直轄市的行政區劃代碼。如果為18位,允許為18位數字,如出現字母只能在最后一位,且僅能為“X”;18位中包含年月的字段滿足日期的構成規則;前兩位滿足省/直轄市的行政區劃代碼;最后一位校驗位滿足身份證的校驗規則(身份證校驗規則見附錄)。附錄:身份證校驗規則公民身份證號碼校驗公式為RESULT = ∑( A[i] * W[i] ) mod 11。其中,i表示號碼字符從右至左包括校驗碼在內的位置序號;A[i]表示第I位置上的數字的數值;W[i]表示第i位置上的加權因子,其值如下:i 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2W[i] 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2RESULT 0 1 2 3 4 5 6 7 8 9 10校驗碼A[1] 1 0 X 9 8 7 6 5 4 3 2*/public static boolean idCardValidate(String idCard) {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"};String ai = "";String ai1 = "";String ai2 = "";// 號碼的長度 15位或18位if (idCard.length() != 15 && idCard.length() != 18) {return false;}// 數字 除最后以為都為數字if (idCard.length() == 18) {ai = idCard.substring(0, 17);} else if (idCard.length() == 15) {ai = idCard.substring(0, 6) + "19" + idCard.substring(6, 15);}if (!isNumeric(ai)) {return false;}// 出生年月是否有效String strYear = ai.substring(6, 10); // 年份String strMonth = ai.substring(10, 12); // 月份String strDay = ai.substring(12, 14); // 月份if (!isDataFormat(strYear + "-" + strMonth + "-" + strDay)) {return false;}GregorianCalendar gc = new GregorianCalendar();SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");try {if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150|| (gc.getTime().getTime() - s.parse(strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {return false;}} catch (Exception e) {e.printStackTrace();}if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {return false;}if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {return false;}// 地區碼是否有效Hashtable h = getAreaCode();if (h.get(ai.substring(0, 2)) == null) {return false;}// 判斷最后一位的值int totalmulAiWi = 0;for (int i = 0; i < 17; i++) {totalmulAiWi = totalmulAiWi + Integer.parseInt(String.valueOf(ai.charAt(i))) * Integer.parseInt(wi[i]);}int modValue = totalmulAiWi % 11;String strVerifyCode = valCodeArr[modValue];ai1 = ai + strVerifyCode.toUpperCase();ai2 = ai + strVerifyCode.toLowerCase();if (idCard.length() == 18) {if (!ai1.equals(idCard) && !ai2.equals(idCard)) {return false;}}return true;}private static boolean isNumeric(String str) {Pattern pattern = Pattern.compile("[0-9]*");Matcher isNum = pattern.matcher(str);if (isNum.matches()) {return true;}return false;}private static boolean isDataFormat(String str) {boolean flag = false;String regxStr = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";Pattern pattern1 = Pattern.compile(regxStr);Matcher isNo = pattern1.matcher(str);if (isNo.matches()) {flag = true;}return flag;}private static Hashtable getAreaCode() {Hashtable hashtable = new Hashtable();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;}/*** 方法描述: 根據身份證獲取年齡,性別* 2表示女* 1表示男* @param idNum* @return* String[]* @author husheng*/public static String[] getAgeAndSexById(String idNum) {String age = "";String sex = "";GregorianCalendar calendar = new GregorianCalendar(TimeZone.getDefault());//獲取系統當前時間int currentYear = calendar.get(Calendar.YEAR);if (idNum.matches("^\\d{15}$|^\\d{17}[\\dxX]$")) {if (idNum.length() == 18) {Pattern pattern = Pattern.compile("\\d{6}(\\d{4})\\d{6}(\\d{1})[\\dxX]{1}");Matcher matcher = pattern.matcher(idNum);if (matcher.matches()) {age = String.valueOf(currentYear - Integer.parseInt(matcher.group(1)));sex = "" + Integer.parseInt(matcher.group(2)) % 2;}} else if (idNum.length() == 15) {Pattern p = Pattern.compile("\\d{6}(\\d{2})\\d{5}(\\d{1})\\d{1}");Matcher m = p.matcher(idNum);if (m.matches()) {int year = Integer.parseInt(m.group(1));year = 2000 + year;if (year > 2020) {year = year - 100;}age = String.valueOf(currentYear - year);sex = "" + Integer.parseInt(m.group(2)) % 2;}}}if ("0".equals(sex)) {sex = "2";}return new String[]{age, sex};}/*** 方法描述: 根據身份證獲取性別* 2表示女* 1表示男* @param idNum* @return* String[]* @author husheng*/public static String getSexByIdCard(String idNum) {String sex = "";GregorianCalendar calendar = new GregorianCalendar(TimeZone.getDefault());//獲取系統當前時間int currentYear = calendar.get(Calendar.YEAR);if (idNum.matches("^\\d{15}$|^\\d{17}[\\dxX]$")) {if (idNum.length() == 18) {Pattern pattern = Pattern.compile("\\d{6}(\\d{4})\\d{6}(\\d{1})[\\dxX]{1}");Matcher matcher = pattern.matcher(idNum);if (matcher.matches()) {sex = "" + Integer.parseInt(matcher.group(2)) % 2;}} else if (idNum.length() == 15) {Pattern p = Pattern.compile("\\d{6}(\\d{2})\\d{5}(\\d{1})\\d{1}");Matcher m = p.matcher(idNum);if (m.matches()) {int year = Integer.parseInt(m.group(1));year = 2000 + year;if (year > 2020) {year = year - 100;}sex = "" + Integer.parseInt(m.group(2)) % 2;}}}if ("0".equals(sex)) {sex = "2";}return sex;}//從身份證中提出出生日期public static Date getBirthDay(String idCard) {String ai = "";Date revertDate = new Date();if (idCard.length() == 18) {ai = idCard.substring(0, 17);} else if (idCard.length() == 15) {ai = idCard.substring(0, 6) + "19" + idCard.substring(6, 15);}// 出生年月是否有效String strYear = ai.substring(6, 10); // 年份String strMonth = ai.substring(10, 12); // 月份String strDay = ai.substring(12, 14); // 月份String birthDay = strYear + "-" + strMonth + "-" + strDay;SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");try {revertDate = sdf.parse(birthDay);} catch (ParseException e) {e.printStackTrace();}return revertDate;}}總結
以上是生活随笔為你收集整理的java基础-对15位或18位身份证号码的验证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 面向对象(二)练习篇之对象数组问题
- 下一篇: 安保人脸识别技术与视频监控系统的结合应用