本篇文章给大家带来了关于java的相关知识,很多项目业务都会设计到人员信息,那么身份证号就是必不可少的校验项,下面这篇文章主要给大家介绍了关于java验证身份证号码有效性的相关资料,希望对大家有帮助。
推荐学习:《java视频教程》
一、身份证结构和形式在通用的身份证号码有15位的和18位的;
15位身份证号码各位的含义:
1、1-2位省、自治区、直辖市代码;
2、3-4位地级市、盟、自治州代码;
3、5-6位县、县级市、区代码;
4、7-12位出生年月日,比如670401代表1967年4月1日,与18位的第一个区别;
5、13-15位为顺序号,其中15位男为单数,女为双数;
18位身份证号码各位的含义:
1、 1-2位表示省(自治区、直辖市、特别行政区)。
2、 3-4位表示市(地区、自治州、盟及国家直辖市所属市辖区和县的汇总码)。其中,01-20,51-70表示省直辖市;21-50表示地区(自治州、盟)。
3、 5-6位表示县(市辖区、县级市、旗)。01-18表示市辖区或地区(自治州、盟)辖县级市;21-80表示县(旗);81-99表示省直辖县级市。
4、 7-14位【生日期码】表示编码对象出生的年、月、日,其中年份用四位数字表示,年、月、日之间不用分隔符。例如:1981年05月11日就用19810511表示。
5、 15-17位【顺序码】表示地址码所标识的区域范围内,对同年、月、日出生的人员编定的顺序号。其中第十七位奇数分给男性,偶数分给女性。
6、 18位【校验码】,作为尾号的校验码,是由号码编制单位按统一的公式计算出来的,如果某人的尾号是0-9,都不会出现x,但如果尾号是10,那么就得用x来代替,因为如果用10做尾号,那么此人的身份证就变成了19位,而19位的号码违反了国家标准,并且中国的计算机应用系统也不承认19位的身份证号码。ⅹ是罗马数字的10,用x来代替10,可以保证公民的身份证符合国家标准。
二、 18位身份证号码计算方法1、将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2。
2、将这17位数字和系数相乘的结果相加。
3、用加出来和除以11,看余数是多少?
4、余数只可能有0-1-2-3-4-5-6-7-8-9-10这11个数字。其分别对应的最后一位身份证的号码为1-0-x -9-8-7-6-5-4-3-2。
5、通过上面得知如果余数是3,就会在身份证的第18位数字上出现的是9。如果对应的数字是2,身份证的最后一位号码就是罗马数字x。
例如:某男性的身份证号码为【53010219200508011x】, 我们看看这个身份证是不是合法的身份证。
首先我们得出前17位的乘积和【(57)+(39)+(010)+(15)+(08)+(24)+(12)+(91)+(26)+(03)+(07)+(59)+(010)+(85)+(08)+(14)+(1*2)】是189,然后用189除以11得出的结果是189/11=17----2,也就是说其余数是2。最后通过对应规则就可以知道余数2对应的检验码是x。所以,可以判定这是一个正确的身份证号码。
以上部分内容来自百度百科
三、java 校验身份证号码package cn.wje.internationa;import java.text.dateformat;import java.text.parseexception;import java.text.simpledateformat;import java.util.calendar;import java.util.date;import java.util.hashmap;import java.util.map;import java.util.regex.pattern;/** * @author qifeng·luo */public class idcardutil { /** * 数字 */ public final static pattern numbers = pattern.compile("\\d+"); /** * 中国公民身份证号码最小长度。 */ private static final int china_id_min_length = 15; /** * 中国公民身份证号码最大长度。 */ private static final int china_id_max_length = 18; public static exception isvalidatedallidcard(string idcard) throws exception { boolean ret = isidcard(idcard); if (!ret) { throw new exception("身份证格式有误"); } return null; } final static map<integer, string> zonenum = new hashmap<>(); /** * 身份证省份编码 * */ static { zonenum.put(11, "北京"); zonenum.put(12, "天津"); zonenum.put(13, "河北"); zonenum.put(14, "山西"); zonenum.put(15, "内蒙古"); zonenum.put(21, "辽宁"); zonenum.put(22, "吉林"); zonenum.put(23, "黑龙江"); zonenum.put(31, "上海"); zonenum.put(32, "江苏"); zonenum.put(33, "浙江"); zonenum.put(34, "安徽"); zonenum.put(35, "福建"); zonenum.put(36, "江西"); zonenum.put(37, "山东"); zonenum.put(41, "河南"); zonenum.put(42, "湖北"); zonenum.put(43, "湖南"); zonenum.put(44, "广东"); zonenum.put(45, "广西"); zonenum.put(46, "海南"); zonenum.put(50, "重庆"); zonenum.put(51, "四川"); zonenum.put(52, "贵州"); zonenum.put(53, "云南"); zonenum.put(54, "西藏"); zonenum.put(61, "陕西"); zonenum.put(62, "甘肃"); zonenum.put(63, "青海"); zonenum.put(64, "宁夏"); zonenum.put(65, "新疆"); zonenum.put(71, "台湾"); zonenum.put(81, "香港"); zonenum.put(82, "澳门"); zonenum.put(91, "国外"); } /** * 校验码 */ final static int[] paritybit = { '1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2' }; /** * 加权因子wi */ final static int[] power_list = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; /** * 验证身份证号有效性 * * @param idcard:身份证号 * @return true/false */ public static boolean isidcard(string idcard) { // 号码长度应为15位或18位 if (idcard == null || (idcard.length() != 15 && idcard.length() != 18)) { return false; } // 校验区位码 if (!zonenum.containskey(integer.valueof(idcard.substring(0, 2)))) { return false; } // 校验年份 string year = idcard.length() == 15 ? "19" + idcard.substring(6, 8) : idcard.substring(6, 10); final int iyear = integer.parseint(year); if (iyear < 1900 || iyear > calendar.getinstance().get(calendar.year)) { // 1900年的pass,超过今年的pass return false; } // 校验月份 string month = idcard.length() == 15 ? idcard.substring(8, 10) : idcard.substring(10, 12); final int imonth = integer.parseint(month); if (imonth < 1 || imonth > 12) { return false; } // 校验天数 string day = idcard.length() == 15 ? idcard.substring(10, 12) : idcard.substring(12, 14); final int iday = integer.parseint(day); if (iday < 1 || iday > 31) { return false; } // 校验一个合法的年月日 if (!isvaliddate(year + month + day)) { return false; } // 校验位数 int power = 0; final char[] cs = idcard.touppercase().tochararray(); for (int i = 0; i < cs.length; i++) {// 循环比正则表达式更快 if (i == cs.length - 1 && cs[i] == 'x') { break;// 最后一位可以是x或者x } if (cs[i] < '0' || cs[i] > '9') { return false; } if (i < cs.length - 1) { power += (cs[i] - '0') * power_list[i]; } } // 校验“校验码” if (idcard.length() == 15) { return true; } return cs[cs.length - 1] == paritybit[power % 11]; } /** * 判断字符串是否为日期格式(合法) * * @param indate:字符串时间 * @return true/false */ public static boolean isvaliddate(string indate) { if (indate == null) { return false; } // 或yyyy-mm-dd simpledateformat dataformat = new simpledateformat("yyyymmdd"); if (indate.trim().length() != dataformat.topattern().length()) { return false; } // 该方法用于设置calendar严格解析字符串;默认为true,宽松解析 dataformat.setlenient(false); try { dataformat.parse(indate.trim()); } catch (parseexception e) { return false; } return true; } /** * 转换成日期 * @param birthday * @return */ private static date tobirthday(string birthday){ try{ calendar calendar = calendar.getinstance(); calendar.set(calendar.year, integer.parseint(birthday.substring(0, 4))); // 月份从0开始,所以减1 calendar.set(calendar.month, integer.parseint(birthday.substring(4, 6)) - 1); calendar.set(calendar.day_of_month, integer.parseint(birthday.substring(6, 8))); // 以下设置时分秒,但是对生日的意义不大 calendar.set(calendar.hour_of_day, 0); calendar.set(calendar.minute, 0); calendar.set(calendar.second, 0); calendar.set(calendar.millisecond, 0); return calendar.gettime(); }catch (exception e){ return null; } } /** * 给定内容是否匹配正则 * * @param pattern 模式 * @param content 内容 * @return 正则为null或者""则不检查,返回true,内容为null返回false */ private static boolean ismatch(pattern pattern, charsequence content) { if (content == null || pattern == null) { // 提供null的字符串为不匹配 return false; } return pattern.matcher(content).matches(); } /** * 将字符串转换成指定格式的日期 * * @param str 日期字符串. * @param dateformat 日期格式. 如果为空,默认为:yyyy-mm-dd hh:mm:ss. * @return */ private static date strtodate(final string str, string dateformat) { if (str == null || str.trim().length() == 0) { return null; } try { if (dateformat == null || dateformat.length() == 0) { dateformat = "yyyy-mm-dd hh:mm:ss"; } dateformat fmt = new simpledateformat(dateformat); return fmt.parse(str.trim()); } catch (exception ex) { return null; } } /** * 根据日期获取年 * * @param date 日期 * @return 年的部分 */ public static int year(date date) { calendar ca = calendar.getinstance(); ca.settime(date); return ca.get(calendar.year); } /** * 将power和值与11取模获得余数进行校验码判断 * * @param isum 加权和 * @return 校验位 */ private static char getcheckcode18(int isum) { switch (isum % 11) { case 10: return '2'; case 9: return '3'; case 8: return '4'; case 7: return '5'; case 6: return '6'; case 5: return '7'; case 4: return '8'; case 3: return '9'; case 2: return 'x'; case 1: return '0'; case 0: return '1'; default: return ' '; } } /** * 获得18位身份证校验码 * 计算方式: * 将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 * 将这17位数字和系数相乘的结果相加 * 用加出来和除以11,看余数是多少 * 余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 x 9 8 7 6 5 4 3 2 * 通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的ⅹ。如果余数是10,身份证的最后一位号码就是2 * @param code17 18位身份证号中的前17位 * @return 第18位 */ private static char getcheckcode18(string code17) { int sum = getpowersum(code17.tochararray()); return getcheckcode18(sum); } /** * 将身份证的每位和对应位的加权因子相乘之后,再得到和值 * * @param iarr 身份证号码的数组 * @return 身份证编码 */ private static int getpowersum(char[] iarr) { int isum = 0; if (power_list.length == iarr.length) { for (int i = 0; i < iarr.length; i++) { isum += integer.valueof(string.valueof(iarr[i])) * power_list[i]; } } return isum; } /** * 将15位身份证号码转换为18位 * * @param idcard 15位身份编码 * @return 18位身份编码 */ public static string convertidcard(string idcard) { stringbuilder idcard18; if (idcard.length() != china_id_min_length) { return null; } if (ismatch(numbers, idcard)) { // 获取出生年月日 string birthday = idcard.substring(6, 12); date birthdate = strtodate(birthday, "yymmdd"); // 获取出生年 int syear = year(birthdate); // 理论上2000年之后不存在15位身份证,可以不要此判断 if (syear > 2000) { syear -= 100; } idcard18 = new stringbuilder().append(idcard, 0, 6).append(syear).append(idcard.substring(8)); // 获取校验位 char sval = getcheckcode18(idcard18.tostring()); idcard18.append(sval); } else { return null; } return idcard18.tostring(); } /** * 从身份证号码中获取生日 * @param idno * @return null表示idno错误,未获取到生日 */ public static date getbirthday(string idno){ if(!isidcard(idno)){ return null; } if (idno.length() == 15) { // 如果是15位转为18位 idno = convertidcard(idno); } return tobirthday(idno.substring(6, 14)); } /** * 从身份证号码中获取生日 * @param idno * @return null表示idno错误,未获取到生日 日期格式为:yyyy-mm-dd */ public static string getbirthdaystr(string idno){ if(!isidcard(idno)){ return null; } if (idno.length() == 15) { // 如果是15位转为18位 idno = convertidcard(idno); } date birthday = tobirthday(idno.substring(6, 14)); simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd"); return simpledateformat.format(birthday); } /** * 从身份证号中获取性别 * @param idno * @return 0:男,1:女,-1:证件号码错误 */ public static string getgender(string idno){ if(!isidcard(idno)){ return "-1"; } if (idno.length() == 15) { // 如果是15位转为18位 idno = convertidcard(idno); } // 奇男,偶女 return (integer.parseint(idno.substring(16, 17)) % 2) == 0 ? "1" : "0"; } /** * 方法调用测试 * */ public static void main(string[] args) { string idc= "130503670401001"; //检查身份证是否合规 boolean idcard = isidcard(idc); if (idcard) { system.out.println("身份证号码合规"); // 获取身份证号码中的生日 date birthday = getbirthday(idc); system.out.println("当前身份证的生日为:"+ getbirthdaystr(idc)); // 获取性别 string gender = getgender(idc); if ("0".equals(gender)) { system.out.println("当前身份证的性别为:男性"); } else if ("1".equals(gender)) { system.out.println("当前身份证的性别为:女性"); } else { system.out.println("当前身份证格式不正确"); } }else { system.out.println("身份证格式有误"); } }}
说明:以上工具中 main 方法是用于测试的,如果放入项目中,不能使用 main 方法测试,可以使用@test 注解测试。此处用 main 方法,只是为了方便贴代码。
结果:
补充:java开发身份证号校验(边输入边校验)公司最近有个需求,就是边输入边校验身份证号码是否合规。以往的校验都是输完够18位就校验,做起来简单的很,头一次听说要求这样搞的,搞了我一下午。
#代码利用多个正则表达式去校验字符
#判断年月日校验
#大小月校验
#闰年闰月的校验
####以下就是代码部分
/** * 功能:身份证的有效验证 * @param idstr 身份证号 * @return true 有效:false 无效 */ public static boolean idcardvalidate(string idstr) { try { int index = 1; //第一位不能为0 if (idstr.length() >= index && idstr.indexof("0") == 0) { return false; } //地区码 index++; if (idstr.length() >= index) { hashtable h = getareacode(); if (h.get(idstr.substring(0, index)) == null) { //errorinfo = "身份证地区编码错误。"; return false; } } // 年份 index = 6; //第一位只能是1和2 if (!verify(idstr, index, "[1,2]")) { return false; } index++; //第二位跟随第一位只有9或0 if (!verify(idstr, index,idstr.length()>index && idstr.substring(6,7).equals("1")?"[9]":"[0]")) { return false; } index++; //第三位 千禧年后0-?,千禧年前0-9 if (!verify(idstr, index,idstr.length()>index && idstr.substring(6,7).equals("1")?"[0-9]":"[0-2]")) { return false; } index++; //第三位 0-9 if (!verify(idstr, index, "[0-9]")) { return false; } if (idstr.length() > index) { //是否比当前年份大 simpledateformat ydf = new simpledateformat("yyyy"); if (ydf.parse(idstr.substring(6, 10)).gettime() > new date().gettime()) { return false; } } // 月份 index++; //第一位只能是1和0 if (!verify(idstr, index, "[0,1]")) { return false; } index++; //第二位跟随第一位变化 if (!verify(idstr, index, idstr.length()>index && idstr.substring(index-1,index).equals("1")?"[0-2]":"[1-9]")) { return false; } if (idstr.length() > index) { //是否比当前月份大 simpledateformat ydf = new simpledateformat("yyyymm"); if (ydf.parse(idstr.substring(6, 12)).gettime() > new date().gettime() && verifymonth(idstr.substring(10, 12))) { return false; } } // ================ 日份 ================ index++; //第一位 二月最多是2 if (!verify(idstr, index, idstr.length()>index && (daymonth(idstr.substring(10, 12)) == 2)?"[0-2]":"[0-3]")) { return false; } index++; if (idstr.length()>index) { int ten = integer.parseint(idstr.substring(index-1, index));//上一位 string filter = "[0-9]"; switch (daymonth(idstr.substring(10, 12))){ case 1://31天 if(ten == 3){ filter = "[0,1]"; } break; case 2://2月 if(ten == 2){ filter = "[0-8]"; int year = integer.parseint(idstr.substring(6, 10)); //闰年 if(year%400 == 0 || year%4==0){ filter = "[0-9]"; } } break; case 3://30天 if(ten == 3){ filter = "[0]"; } break; } if(ten == 0){ filter = "[1-9]"; } if(!verifyindex(idstr,index,filter)){ return false; } } if (idstr.length() > index) { simpledateformat ydf = new simpledateformat("yyyymmdd"); //是否比当前日期大 if (ydf.parse(idstr.substring(6, 14)).gettime() > new date().gettime()) { return false; } } // 号码的长度 string filter = "[0-9]{0,17}"; boolean flag = idstr.matches(filter + (idstr.length() == 18 ? "[0-9,x,x]" : "")); if (!flag) { return false; } } catch (parseexception e) { e.printstacktrace(); return false; } return true; } /** * 验证 * @param text 文本 * @param index 下标 * @param filter 正则匹配 * @return */ private static boolean verify(string text, int index, string filter) { //是否满足长度 if (text.length() > index) { return verifyindex(text, index, filter); } return true; } /** * 验证 * @param text 文本 * @param index 下标 * @param filter 正则匹配 * @return */ private static boolean verifyindex(string text, int index, string filter) { string sub = text.substring(index, index + 1); return sub.matches(filter); } private static boolean verifymonth(string month){ return integer.parseint(month)>12; } /** * 大小月、二月 * @return */ private static int daymonth(string month){ switch (integer.parseint(month)){ case 4: case 6: case 9: case 11: return 3; case 2:return 2; default: return 1; } } /** * 功能:设置地区编码 * @return hashtable 对象 */ 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; }
推荐学习:《java视频教程》
以上就是代码实例:java验证身份证号码有效性的详细内容。