Java identity card Verification and regular expression parsing

Source: Internet
Author: User
Tags dateformat

Java identity card Verification and regular expression parsing

 PackageService;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.regex.Pattern;Importorg.junit.Test; Public classTestregex { Public Static Booleanischinesename (String realname) {returnPattern.matches ("[\u4e00-\u9fa5|] {2,15} ", Realname); }     Public Static BooleanIscardid (String cardid) {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}) $ ", Cardid); }     Public StaticString[] Randomarray (intlength) {        intindex = 0; String Temp= ",";  for(intii=0;ii<1000;ii++){             for(inti=0;i<length;i++) {Index= (int) (Math.random () *10); Temp+ = (Index + ""); } temp+=","; }        returnTemp.split (","); }    //Pre-ID 1-bit weighted factor per bit    Private Static int[] Power = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; //ID card 18th School check-in code    Private StaticString[] Refnumber ={"1", "0", "X", "9", "8", "7", "6", "5", "4", "3"}; //Province (municipality) code table    PrivateString provincecode[] = {"11", "12", "13", "14", "15", "21", "22",               "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43",               "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63",               "64", "65", "71", "81", "82", "91" }; /*** Check the province information of the identity card is correct (use with 18/15-digit ID) *@paramProvinceid *@return     */     Public BooleanCheckprovinceid (String provinceid) { for(String id:provincecode) {if(Id.equals (Provinceid)) {return true; }        }           return false; }    /*** Verify that the 18th digit of the ID card is correct (only for 18-digit ID) *@paramCardId *@return     */     Public Static Booleancheckcardidlastnum (String cardId) {if(Cardid.length ()! = 18){            return false; }        Char[] tmp =Cardid.tochararray (); int[] Cardidarray =New int[Tmp.length-1]; intI=0;  for(i=0;i<tmp.length-1;i++) {Cardidarray[i]= Integer.parseint (tmp[i]+ ""); } String Checkcode=Sumpower (Cardidarray); String Lastnum= Tmp[tmp.length-1] + ""; if(Lastnum.equals ("X") ) {Lastnum=lastnum.touppercase (); }        if(!checkcode.equals (Lastnum)) {            return false; }        return true; }    /*** To calculate the 18th digit Check code of the ID card *@paramCardidarray *@return     */     Public StaticString Sumpower (int[] cardidarray) {        intresult = 0;  for(inti=0;i<power.length;i++) {result+ = power[i] *Cardidarray[i]; }        returnrefnumber[(result%11)]; }    /*** Determine if the date is valid *@paramIndate *@return     */      Public Booleanisvaliddate (String indate) {if(Indate = =NULL){          return false; } SimpleDateFormat DateFormat=NewSimpleDateFormat ("YyyyMMdd"); if(Indate.trim (). Length ()! =Dateformat.topattern (). Length ()) {              return false; } dateformat.setlenient (false);//Strict date Matching        Try{dateformat.parse (Indate.trim ()); } Catch(ParseException e) {return false; }        return true; }    //@Test     Public voidtestname () {string[] array= {"Clock •", "where"};  for(inti=0;i<array.length;i++) {System.out.println (Array[i]+":"+Ischinesename (Array[i])); }    }    //       //@Test     Public voidTestcardid () {string[] array= Randomarray (18); intindex = 0,sum =0;  for(inti=0;i<array.length;i++) {sum+ = 1; if(Iscardid (Array[i])) {Index+ = 1; } System.out.println (Array[i]+":"+Iscardid (Array[i])); }        DoubleRET = index/sum; SYSTEM.OUT.PRINTLN (Index+ "," +sum+ "," +ret); }    //@Test     Public voidTestarray () {string[] array= Randomarray (15);  for(inti=0;i<array.length;i++) {System.out.println (array[i]); }    }    //@Test     Public voidTestlastnum () {string[] cardId= {"440982199563272359", "43010219890414298x"};  for(inti=0;i<cardid.length;i++) {System.out.println (Cardid[i]+":"+Checkcardidlastnum (Cardid[i])); }    }    //@Test     Public voidtestprocince () {string[] cardId= {"440982199563272359", "43010219890414298x"};  for(inti=0;i<cardid.length;i++) {System.out.println (cardid[i].substring (0,2) + ":" +checkprovinceid (Cardid[i].substring (0,2))); }} @Test Public voidtestdate () {//string[] cardId = {"440982199003272359", "43010219890414298x"};String cardid[] = {"445102198904312332",                "131100200102290696",                "51042119710630173X",                "340823196806263613",                "340703197410050615",                "440982195201230696",                "130203196110239132",                "321101197411218776",                "340501196812265178",                "330424197204277791",                "61040219710929439X",                "230826195610286476",                "350305196501252819",                "450881197212026374",                "220282197010121032",                "341723197209084377",                "451024197211265090",                "330727195609144416",                "411626197504219395",                "230605196203286219",                "21068119520426399X",                "511821195404264774",                "622923197107299798",        };  for(inti=0;i<cardid.length;i++) {System.out.println (cardid[i].substring (6,14) + ":" +isvaliddate (Cardid[i].substring (6,14))); }    }}

Java identity card Verification and regular expression parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.