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 "));
}
}