An object's 15-and 18-digit Chinese mainland ID card number parsing and verification tool

Source: Internet
Author: User

Import Java. text. simpledateformat; import Java. util. date;/*** ID card number, which can parse fields of the ID card number and verify whether the ID card number is valid <br> * ID card number composition: 6-digit address code + 8-digit birthday code + 3-digit sequence code + 1-digit Verification Code ** @ author liuex **/public class idcard {/*** complete ID card number */private final string cardNumber; // check whether the cached ID card is valid because the verification validity is frequently used and the calculation of complicated private Boolean cachevalidateresult = NULL; // cache the birth date, because the birth date is frequently used and complicated calculation is private date cachebirthdate = NULL; Public Boolean validate () {If (null = CAC Hevalidateresult) {boolean result = true; // ID card number cannot be blank result = Result & (null! = CardNumber); // The length of the ID card number is 18 (New certificate) Result = Result & new_card_number_length = cardNumber. length (); // the first 17 digits of the ID card number must be Arabic numerals for (INT I = 0; Result & I <new_card_number_length-1; I ++) {char CH = cardNumber. charat (I); Result = Result & ch> = '0' & Ch <= '9 ';} // The 18th-digit verification of the ID card number is correct. Result = Result & (calculateverifycode (cardNumber) = cardNumber. charat (new_card_number_length-1); // The date of birth cannot be later than the current time, and cannot be earlier than 1900 try {Date birthdate = This. getbirthdate (); Result = Result & null! = Birthdate; Result = Result & birthdate. before (new date (); Result = Result & birthdate. after (minimal_birth_date);/*** the year, month, and day of the birth date must be correct. For example, the month range is [], and the date range is []. you also need to verify that * the month and date are consistent with */string birthdaypart = This. getbirthdaypart (); string realbirthdaypart = This. createbirthdateparser (). format (birthdate); Result = Result & (birthdaypart. equals (realbirthdaypart);} catch (exception e) {result = False;} // cachevalidateresult = Boolean. valueof (result);} return cachevalidateresult;}/*** if it is a 15-digit ID card number, it is automatically converted to 18-digit ** @ Param cardNumber */Public idcard (string cardNumber) {If (null! = CardNumber) {cardNumber = cardNumber. trim (); If (old_card_number_length = cardNumber. length () {cardNumber = conterttonewcardnumber (cardNumber) ;}} this. cardNumber = cardNumber;} Public String getcardnumber () {return cardNumber;} Public String getaddresscode () {This. checkifvalid (); return this. cardNumber. substring (0, 6);} public date getbirthdate () {If (null = This. cachebirthdate) {try {This. cachebirt Hdate = This. createbirthdateparser (). parse (this. getbirthdaypart ();} catch (exception e) {Throw new runtimeexception ("the birth date of the ID card is invalid");} return new date (this. cachebirthdate. gettime ();} public Boolean ismale () {return 1 = This. getgendercode ();} public Boolean isfemal () {return false = This. ismale ();}/*** gets the ID card's 17th digits. The odd number is male, and the even number is female ** @ return */private int getgendercode () {This. checkifvalid (); char gender Code = This. cardNumber. charat (new_card_number_length-2); Return (INT) (gendercode-'0') & 0x1);} private string getbirthdaypart () {return this. cardNumber. substring (6, 14);} private simpledateformat createbirthdateparser () {return New simpledateformat (birth_date_format);} private void checkifvalid () {If (false = This. validate () {Throw new runtimeexception ("Incorrect ID number! ") ;}}// The birthdate format in the ID card number is private final static string birth_date_format =" yyyymmdd "; // The minimum birthdate of the ID card, january 1, 1900 private final static date minimal_birth_date = new date (-2209017600000l); Private Final Static int new_card_number_length = 18; private final static int old_card_number_length = 15; /*** last verification code in 18-digit ID card */private final static char [] verify_code = {'1', '0', 'x', '9 ', '8', '7', '6', '5', '4', '3', '2 '};/*** Weight of each digit in the 18-digit ID card */private final static int [] verify_code_weight = {7, 9, 10, 5, 8, 4, 2, 18th, 3, 7, 9, 10, 5, 8, 4, 2};/*** <li> check code (digits ): <br/> * <ul> * <li> the weighted summation formula S = sum (ai * WI), I = 0... 16. First, sum the weights of the first 17 digits. * Ai: indicates the ID card number at position I. Digital value wi: indicates the Weighted Factor wi at position I: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4*2; </LI> * <li> computing mode y = Mod (S, 11) </LI> * <li> use the modulo to obtain the corresponding verification code Y: 0 1 2 3 4 5 6 7 8 9 10.: 1 0x9 8 7 6 5 4 3 2 </LI> * </ul> ** @ Param cardNumber * @ return */Private Static char calculateverifycode (charsequence cardNumber) {int sum = 0; For (INT I = 0; I <new_card_number_length-1; I ++) {char CH = cardNumber. charat (I); sum + = (INT) (CH-'0') * verify_code_weight [I];} return verify_code [Sum % 11];} /*** convert the 15-digit ID card number to the 18-digit ID card number <br> * the difference between the 15-digit ID card number and the 18-digit ID card number is: <br> In * 1 and 15 ID card numbers, the "Year of birth" field is two characters During conversion, you must add "19", indicating that the 20th century <br> * 2 and 15 ID cards do not have the last verification code. In the 18-digit ID card, the verification code generates ** @ Param cardNumber * @ return */Private Static string conterttonewcardnumber (string oldcardnumber) {stringbuilder Buf = new stringbuilder (new_card_number_length ); buf. append (oldcardnumber. substring (0, 6); Buf. append ("19"); Buf. append (oldcardnumber. substring (6); Buf. append (idcard. calculateverifycode (BUF); Return Buf. tostring ();}}

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.