最近有个身份证合法性的检测,网上有很多代码,这里写出来备忘
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; public class IDcheckClassUtil { public static final int CHINA_ID_MIN_LENGTH = 15; public static final int MIN = 1930; public static final int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; public static Map<String, String> cityCodes = new HashMap<String, String>(); static { cityCodes.put("11", "北京"); cityCodes.put("12", "天津"); cityCodes.put("13", "河北"); cityCodes.put("14", "山西"); cityCodes.put("15", "内蒙古"); cityCodes.put("21", "辽宁"); cityCodes.put("22", "吉林"); cityCodes.put("23", "黑龙江"); cityCodes.put("31", "上海"); cityCodes.put("32", "江苏"); cityCodes.put("33", "浙江"); cityCodes.put("34", "安徽"); cityCodes.put("35", "福建"); cityCodes.put("36", "江西"); cityCodes.put("37", "山东"); cityCodes.put("41", "河南"); cityCodes.put("42", "湖北"); cityCodes.put("43", "湖南"); cityCodes.put("44", "广东"); cityCodes.put("45", "广西"); cityCodes.put("46", "海南"); cityCodes.put("50", "重庆"); cityCodes.put("51", "四川"); cityCodes.put("52", "贵州"); cityCodes.put("53", "云南"); cityCodes.put("54", "西藏"); cityCodes.put("61", "陕西"); cityCodes.put("62", "甘肃"); cityCodes.put("63", "青海"); cityCodes.put("64", "宁夏"); cityCodes.put("65", "新疆"); cityCodes.put("71", "台湾"); cityCodes.put("81", "香港"); cityCodes.put("82", "澳门"); cityCodes.put("91", "国外"); } /** 中国公民身份证号码最大长度 */ public static final int CHINA_ID_MAX_LENGTH = 18; public static boolean validateCard(String idCard) { String card = idCard.trim(); if (validateIdCard18(card)) { return true; } if (validateIdCard15(card)) { return true; } return false; } public static void main(String[] args) { System.out.print(validateCard("330621198903134673")); } /**检测是否全是0~9的数字*/ public static boolean isNum(String val) { return val == null || "".equals(val) ? false : val.matches("^[0-9]*$"); } public static int[] converCharToInt(char[] ca) { int len = ca.length; int[] iArr = new int[len]; try { for (int i = 0; i < len; i++) { iArr[i] = Integer.parseInt(String.valueOf(ca[i])); } } catch (NumberFormatException e) { e.printStackTrace(); } return iArr; } public static boolean validateIdCard18(String idCard) { boolean bTrue = false; if (idCard.length() == CHINA_ID_MAX_LENGTH) { // 取出从第一位到底17位的号码用于计算 String code17 = idCard.substring(0, 17); // 获取第十八位的code String code18 = idCard.substring(17, CHINA_ID_MAX_LENGTH); if (isNum(code17)) { char[] cArr = code17.toCharArray(); if (cArr != null) { int[] iCard = converCharToInt(cArr); int iSum17 = getPowerSum(iCard); // 获取校验码 String val = getCheckCode18(iSum17); if (val.length() > 0) { /**如果通过计算得到的code和第十八位的code一样则是正确的身份证号码*/ if (val.equalsIgnoreCase(code18)) { bTrue = true; } } } } } return bTrue; } /**加权重计算和 最后还要对11取余 用于和最后一位校验码判断*/ public static int getPowerSum(int[] iArr) { int iSum = 0; if (power.length == iArr.length) { for (int i = 0; i < iArr.length; i++) { for (int j = 0; j < power.length; j++) { if (i == j) { iSum = iSum + iArr[i] * power[j]; } } } } return iSum; } /**获取校验码*/ public static String getCheckCode18(int iSum) { String sCode = ""; switch (iSum % 11) { case 10: sCode = "2"; break; case 9: sCode = "3"; break; case 8: sCode = "4"; break; case 7: sCode = "5"; break; case 6: sCode = "6"; break; case 5: sCode = "7"; break; case 4: sCode = "8"; break; case 3: sCode = "9"; break; case 2: sCode = "x"; break; case 1: sCode = "0"; break; case 0: sCode = "1"; break; } return sCode; } /**15位身份证号检测*/ public static boolean validateIdCard15(String idCard) { if (idCard.length() != CHINA_ID_MIN_LENGTH) { return false; } if (isNum(idCard)) { String proCode = idCard.substring(0, 2); if (cityCodes.get(proCode) == null) { return false; } String birthCode = idCard.substring(6, 12); Date birthDate = null; try { birthDate = new SimpleDateFormat("yy").parse(birthCode .substring(0, 2)); } catch (ParseException e) { e.printStackTrace(); } Calendar cal = Calendar.getInstance(); if (birthDate != null) cal.setTime(birthDate); if (!valiDate(cal.get(Calendar.YEAR), Integer.valueOf(birthCode.substring(2, 4)), Integer.valueOf(birthCode.substring(4, 6)))) { return false; } } else { return false; } return true; } public static boolean valiDate(int iYear, int iMonth, int iDate) { Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int datePerMonth; if (iYear < MIN || iYear >= year) { return false; } if (iMonth < 1 || iMonth > 12) { return false; } switch (iMonth) { case 4: case 6: case 9: case 11: datePerMonth = 30; break; case 2: boolean dm = ((iYear % 4 == 0 && iYear % 100 != 0) || (iYear % 400 == 0)) && (iYear > MIN && iYear < year); datePerMonth = dm ? 29 : 28; break; default: datePerMonth = 31; } return (iDate >= 1) && (iDate <= datePerMonth); } }