Java: ID card number 15-digit 18-digit conversion class

Source: Internet
Author: User

Package cyachina. util;

/**
* Created by intellij idea.
* User: Administrator
* Date: 2004-8-13
* Time: 15:37:28
* To change this template use file | Settings | file templates.
*/
Public class idcardutil {

/**
*
* @ Param century 19 for 19xx and 20 for 20xx
* @ Param idcardno15 ID card number to be converted
* @ Return
*/
Public static string from15to18 (INT century, string idcardno15 ){

String centurystr = "" + century;
If (century <0 | centurystr. Length ()! = 2)
Throw new illegalargumentexception ("invalid century! It should be a positive integer of two digits. ");
If (! (Isidcardno (idcardno15) & idcardno15.length () = 15 ))
Throw new illegalargumentexception ("the old ID card number format is incorrect! ");

Int [] Weight = new int [] {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };

// Add the century code to the new number ontology.
String newnobody = idcardno15.substring (0, 6) + centurystr + idcardno15.substring (6 );

// Calculate the last check code below

Int checksum = 0;
For (INT I = 0; I <17; I ++ ){
Int AI = integer. parseint ("" + newnobody. charat (I); // the value at the I position
Checksum = checksum + AI * weight [I];
}

Int checknum = checksum % 11;
String checkchar = NULL;

Switch (checknum ){
Case 0: checkchar = "1"; break;
Case 1: checkchar = "0"; break;
Case 2: checkchar = "X"; break;
Default: checkchar = "" + (12-checknum );
}

Return newnobody + checkchar;

}

Public static string from18to15 (string idcardno18 ){

If (! (Isidcardno (idcardno18) & idcardno18.length () = 18 ))
Throw new illegalargumentexception ("Incorrect ID card number format! ");

Return idcardno18.substring (0, 6) + idcardno18.substring (8, 17 );
}

/**
* Determine whether the specified string meets the ID card number requirements.
* @ Param Str
* @ Return
*/
Public static Boolean isidcardno (string Str ){

If (STR = NULL)
Return false;

Int Len = Str. Length ();
If (Len! = 15 & Len! = 18)
Return false;

For (INT I = 0; I <Len; I ++ ){
Try {
Integer. parseint ("" + Str. charat (I ));
}
Catch (numberformatexception e ){
Return false;
}
}

Return true;
}

Public static void main (string [] ARGs ){

System. Out. println (from15to18 (19, "xxxxxxxxxxxxxxx "));
System. Out. println (from18to15 ("xxxxxxxxxxxxxxxxxx "));
}

}

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.