java验证身份证号码及编码规则和提取相应信息.docx
《java验证身份证号码及编码规则和提取相应信息.docx》由会员分享,可在线阅读,更多相关《java验证身份证号码及编码规则和提取相应信息.docx(3页珍藏版)》请在冰豆网上搜索。
![java验证身份证号码及编码规则和提取相应信息.docx](https://file1.bdocx.com/fileroot1/2023-6/8/aff8fd19-b737-4493-834b-931682e5666b/aff8fd19-b737-4493-834b-931682e5666b1.gif)
java验证身份证号码及编码规则和提取相应信息
java验证身份证号码及编码规则和提取相应信息
/***将15位的身份证转成18位身份证**@paramidcard*@return*/publicStringconvertIdcarBy15bit(Stringidcard){Stringidcard17=null;//非15位身份证if(idcard.length()!
=15){returnnull;}if(isDigital(idcard)){//获取出生年月日Stringbirthday=idcard.substring(6,12);Datebirthdate=null;try{birthdate=newSimpleDateFormat("yyMMdd").parse(birthday);}catch(ParseExceptione){e.printStackTrace();}Calendarcday=Calendar.getInstance();cday.setTime(birthdate);Stringyear=String.valueOf(cday.get(Calendar.YEAR));idcard17=idcard.substring(0,6)+year+idcard.substring(8);charc[]=idcard17.toCharArray();StringcheckCode="";if(null!
=c){intbit[]=newint[idcard17.length()];//将字符数组转为整型数组bit=converCharToInt(c);intsum17=0;sum17=getPowerSum(bit);//获取和值与11取模得到余数进行校验码checkCode=getCheckCodeBySum(sum17);//获取不到校验位if(null==checkCode){returnnull;}//将前17位与第18位校验码拼接idcard17+=checkCode;}}else{//身份证包含数字returnnull;}returnidcard17;}/***15位和18位身份证号码的基本数字和位数验校**@paramidcard*@return*/publicbooleanisIdcard(Stringidcard){returnidcard==null||"".equals(idcard)?
false:
Pattern.matches("(^\\d{15}$)|(\\d{17}(?
:
\\d|x|X)$)",idcard);}/***15位身份证号码的基本数字和位数验校**@paramidcard*@return*/publicbooleanis15Idcard(Stringidcard){returnidcard==null||"".equals(idcard)?
false:
Pattern.matches("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$",idcard);}/***18位身份证号码的基本数字和位数验校**@paramidcard*@return*/publicbooleanis18Idcard(Stringidcard){returnPattern.matches("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([\\d|x|X]{1})$",idcard);}/***数字验证**@paramstr*@return*/publicbooleanisDigital(Stringstr){returnstr==null||"".equals(str)?
false:
str.matches("^[0-9]*$");}/***将身份证的每位和对应位的加权因子相乘之后,再得到和值**@parambit*@return*/publicintgetPowerSum(int[]bit){intsum=0;if(power.length!
=bit.length){returnsum;}for(inti=0;i<bit.length;i++){for(intj=0;j<power.length;j++){if(i==j){sum=sum+bit[i]*power[j];}}}returnsum;}/***将和值与11取模得到余数进行校验码判断**@paramcheckCode*@paramsum17*@return校验位*/publicStringgetCheckCodeBySum(intsum17){StringcheckCode=null;switch(sum17%11){case10:
checkCode="2";break;case9:
checkCode="3";break;case8:
checkCode="4";break;case7:
checkCode="5";break;case6:
checkCode="6";break;case5:
checkCode="7";break;case4:
checkCode="8";break;case3:
checkCode="9";break;case2:
checkCode="x";break;case1:
checkCode="0";break;case0:
checkCode="1";break;}returncheckCode;}/***将字符数组转为整型数组**@paramc*@return*@throwsNumberFormatException*/publicint[]converCharToInt(char[]c)throwsNumberFormatException{int[]a=newint[c.length];intk=0;for(chartemp:
c){a[k++]=Integer.parseInt(String.valueOf(temp));}returna;}publicstaticvoidmain(String[]args)throwsException{Stringidcard15="";Stringidcard18="";IdcardValidatoriv=newIdcardValidator();booleanflag=false;flag=iv.isValidate18Idcard(idcard18);System.out.println(flag);flag=iv.isValidate15Idcard(idcard15);System.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.javaJava代码importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjava.util.GregorianCalendar;importjava.util.HashMap;importjava.util.Map;importjava.util.Set;/***<p>*类说明:
提取身份证相关信息*</p>*/publicclassIdcardInfoExtractor{//省份privateStringprovince;//城市privateStringcity;//区县privateStringregion;//年份privateintyear;//月份privateintmonth;//日期privateintday;//性别privateStringgender;//出生日期privateDatebirthday;privateMap<String,String>cityCodeMap=newHashMap<String,String>(){{this.put("11","北京");this.put("12","天津");this.put("13","河北");this.put("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("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","国外");}};privateIdcardValidatorvalidator=null;/***通过构造方法初始化各个成员属性*/publicIdcardInfoExtractor(Stringidcard){try{validator=newIdcardValidator();if(validator.isValidatedAllIdcard(idcard)){if(idcard.length()==15){idcard=validator.convertIdcarBy15bit(idcard);}//获取省份StringprovinceId=idcard.substring(0,2);Set<String>key=this.cityCodeMap.keySet();for(Stringid:
key){if(id.equals(provinceId)){this.province=this.cityCodeMap.get(id);break;}}//获取性别Stringid17=idcard.substring(16,17);if(Integer.parseInt(id17)%2!
=0){this.gender="男";}else{this.gender="女";}//获取出生日期Stringbirthday=idcard.substring(6,14);Datebirthdate=newSimpleDateFormat("yyyyMMdd").parse(birthday);this.birthday=birthdate;GregorianCalendarcurrentDay=newGregorianCalendar();currentDay.setTime(birthdate);this.year=currentDay.get(Calendar.YEAR);this.month=currentDay.get(Calendar.MONTH)+1;this.day=currentDay.get(Calendar.DAY_OF_MONTH);}}catch(Exceptione){e.printStackTrace();}}/***@returntheprovince*/publicStringgetProvince(){returnprovince;}/***@returnthecity*/publicStringgetCity(){returncity;}/***@returntheregion*/publicStringgetRegion(){returnregion;}/***@returntheyear*/publicintgetYear(){returnyear;}/***@returnthemonth*/publicintgetMonth(){returnmonth;}/***@returntheday*/publicintgetDay(){returnday;}/***@returnthegender*/publicStringgetGender(){returngender;}/***@returnthebirthday*/publicDategetBirthday(){returnbirthday;}@OverridepublicStringtoString(){return"省份:
"+this.province+",性别:
"+this.gender+",出生日期:
"+this.birthday;}publicstaticvoidmain(String[]args){Stringidcard="";IdcardInfoExtractorie=newIdcardInfoExtractor(idcard);System.out.println(ie.toString());}}