1、java验证身份证号码及编码规则和提取相应信息java验证身份证号码及编码规则和提取相应信息 /* * 将15位的身份证转成18位身份证 * * param idcard * return */ public String convertIdcarBy15bit(String idcard) String idcard17 = null; / 非15位身份证 if (idcard.length() != 15) return null; if (isDigital(idcard) / 获取出生年月日 String birthday = idcard.substring(6, 12); Date
2、 birthdate = null; try birthdate = new SimpleDateFormat(yyMMdd).parse(birthday); catch (ParseException e) e.printStackTrace(); Calendar cday = Calendar.getInstance(); cday.setTime(birthdate); String year = String.valueOf(cday.get(Calendar.YEAR); idcard17 = idcard.substring(0, 6) + year + idcard.subs
3、tring(8); char c = idcard17.toCharArray(); String checkCode = ; if (null != c) int bit = new intidcard17.length(); / 将字符数组转为整型数组 bit = converCharToInt(c); int sum17 = 0; sum17 = getPowerSum(bit); / 获取和值与11取模得到余数进行校验码 checkCode = getCheckCodeBySum(sum17); / 获取不到校验位 if (null = checkCode) return null;
4、/ 将前17位与第18位校验码拼接 idcard17 += checkCode; else / 身份证包含数字 return null; return idcard17; /* * 15位和18位身份证号码的基本数字和位数验校 * * param idcard * return */ public boolean isIdcard(String idcard) return idcard = null | .equals(idcard) ? false : Pattern.matches( (d15$)|(d17(?:d|x|X)$), idcard); /* * 15位身份证号码的基本数字和
5、位数验校 * * param idcard * return */ public boolean is15Idcard(String idcard) return idcard = null | .equals(idcard) ? false : Pattern.matches( 1-9d7(0d)|(10-2)(0|1|2d)|30-1)d3$, idcard); /* * 18位身份证号码的基本数字和位数验校 * * param idcard * return */ public boolean is18Idcard(String idcard) return Pattern .match
6、es( 1-9d51-9d3(0d)|(10-2)(0|1|2d)|30-1)d3(d|x|X1)$, idcard); /* * 数字验证 * * param str * return */ public boolean isDigital(String str) return str = null | .equals(str) ? false : str.matches(0-9*$); /* * 将身份证的每位和对应位的加权因子相乘之后,再得到和值 * * param bit * return */ public int getPowerSum(int bit) int sum = 0;
7、if (power.length != bit.length) return sum; for (int i = 0; i < bit.length; i+) for (int j = 0; j < power.length; j+) if (i = j) sum = sum + biti * powerj; return sum; /* * 将和值与11取模得到余数进行校验码判断 * * param checkCode * param sum17 * return 校验位 */ public String getCheckCodeBySum(int sum17) String c
8、heckCode = null; switch (sum17 % 11) case 10: checkCode = 2; break; case 9: checkCode = 3; break; case 8: checkCode = 4; break; case 7: checkCode = 5; break; case 6: checkCode = 6; break; case 5: checkCode = 7; break; case 4: checkCode = 8; break; case 3: checkCode = 9; break; case 2: checkCode = x;
9、 break; case 1: checkCode = 0; break; case 0: checkCode = 1; break; return checkCode; /* * 将字符数组转为整型数组 * * param c * return * throws NumberFormatException */ public int converCharToInt(char c) throws NumberFormatException int a = new intc.length; int k = 0; for (char temp : c) ak+ = Integer.parseInt
10、(String.valueOf(temp); return a; public static void main(String args) throws Exception String idcard15 = ; String idcard18 = ; IdcardValidator iv = new IdcardValidator(); boolean flag = false; flag = iv.isValidate18Idcard(idcard18); System.out.println(flag); flag = iv.isValidate15Idcard(idcard15); S
11、ystem.out.println(flag); System.out.println(iv.convertIdcarBy15bit(idcard15); flag = iv.isValidate18Idcard(iv.convertIdcarBy15bit(idcard15); System.out.println(flag); System.out.println(iv.isValidatedAllIdcard(idcard18); 提取身份证相关信息:IdcardInfoExtractor.java Java代码 import java.text.SimpleDateFormat; im
12、port java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Map; import java.util.Set; /* * <p> * 类说明:提取身份证相关信息 * </p> */ public class IdcardInfoExtractor / 省份 private String province; / 城市 private String city; / 区县 priva
13、te String region; / 年份 private int year; / 月份 private int month; / 日期 private int day; / 性别 private String gender; / 出生日期 private Date birthday; private Map<String, String> cityCodeMap = new HashMap<String, String>() this.put(11, 北京); this.put(12, 天津); this.put(13, 河北); this.put(14, 山西);
14、 this.put(15, 内蒙古); this.put(21, 辽宁); this.put(22, 吉林); this.put(23, 黑龙江); this.put(31, 上海); this.put(32, 江苏); this.put(33, 浙江); this.put(34, 安徽); this.put(35, 福建); this.put(36, 江西); this.put(37, 山东); this.put(41, 河南); this.put(42, 湖北); this.put(43, 湖南); this.put(44, 广东); this.put(45, 广西); this.put(
15、46, 海南); this.put(50, 重庆); this.put(51, 四川); this.put(52, 贵州); this.put(53, 云南); this.put(54, 西藏); this.put(61, 陕西); this.put(62, 甘肃); this.put(63, 青海); this.put(64, 宁夏); this.put(65, 新疆); this.put(71, 台湾); this.put(81, 香港); this.put(82, 澳门); this.put(91, 国外); ; private IdcardValidator validator = n
16、ull; /* * 通过构造方法初始化各个成员属性 */ public IdcardInfoExtractor(String idcard) try validator = new IdcardValidator(); if (validator.isValidatedAllIdcard(idcard) if (idcard.length() = 15) idcard = validator.convertIdcarBy15bit(idcard); / 获取省份 String provinceId = idcard.substring(0, 2); Set<String> key
17、= this.cityCodeMap.keySet(); for (String id : key) if (id.equals(provinceId) this.province = this.cityCodeMap.get(id); break; / 获取性别 String id17 = idcard.substring(16, 17); if (Integer.parseInt(id17) % 2 != 0) this.gender = 男; else this.gender = 女; / 获取出生日期 String birthday = idcard.substring(6, 14);
18、 Date birthdate = new SimpleDateFormat(yyyyMMdd) .parse(birthday); this.birthday = birthdate; GregorianCalendar currentDay = new GregorianCalendar(); currentDay.setTime(birthdate); this.year = currentDay.get(Calendar.YEAR); this.month = currentDay.get(Calendar.MONTH) + 1; this.day = currentDay.get(C
19、alendar.DAY_OF_MONTH); catch (Exception e) e.printStackTrace(); /* * return the province */ public String getProvince() return province; /* * return the city */ public String getCity() return city; /* * return the region */ public String getRegion() return region; /* * return the year */ public int
20、getYear() return year; /* * return the month */ public int getMonth() return month; /* * return the day */ public int getDay() return day; /* * return the gender */ public String getGender() return gender; /* * return the birthday */ public Date getBirthday() return birthday; Override public String toString() return 省份: + this.province + ,性别: + this.gender + ,出生日期: + this.birthday; public static void main(String args) String idcard = ; IdcardInfoExtractor ie = new IdcardInfoExtractor(idcard); System.out.println(ie.toString();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1