導入Java . text . number format;
導入Java . util . *;
導入Java . io . bytearray inputstream;
導入Java . text . decimal format;
導入Java . text . simple date format;
公共類COMUT {
公共計算機(){
}
/**
*
*將15居民身份證號碼修復為18位數。
*
* @param personIDCode
*
* @返回
*/
公共靜態字符串fixPersonIDCode(字符串PersonIDCode)
{
字符串retIDCode =
if(personIDCode = = null | | personIDCode . trim()。長度()!= 15) {
返回personIDCode
}
string id 17 = personidcode . substring(0,6) + "19 "
+ personIDCode.substring(6,15);// 15身份證補領\'19\ '
// char[] code =
// {\'1\ ',\'0\ ',\'X\ ',\'9\ ',\'8\ ',\'7\ ',\'6\ ',\'5\ ',\'4\ ',\'3\ ',\ ' 2 \ ' };
///+01件。
char[] code = { '1 ',' 0 ',' X ',' 9 ',' 8 ',' 7 ',' 6 ',' 5 ',' 4 ',' 3 ',' 2 ' };// 11
int[] factor = { 0,2,4,8,5,10,9,7,3,6,1,2,4,8,5,10,9,7 };// 18;
int[]idcd = new int[18];
int I;
int j;
int sum
int余數;
for(I = 1;我& lt18;i++)
{
j = 17-I;
idcd[I]= integer . parse int(id 17 . substring(j,j+1));
}
sum = 0;
for(I = 1;我& lt18;i++)
{
sum = sum+idcd[I]* factor[I];
}
余數=總和% 11;
string last check bit = string . value of(code[remainder]);
返回id 17+last check bit;
}
/**
*
*判斷是有效的18位還是15位居民身份證號碼。
*
* @param identityId
*:18或15居民的身份證號碼。
*
* @ return:true:18或15居民的有效身份證號。
*/
公共靜態布爾isIdentityId(String identityId){
if (isEmpty(identityId))
返回false
嘗試{
if(identity id . length()= = 18){
字符串identity id 15 = identity id . substring(0,6)
+ identityId.substring(8,17);
//system . out . println(" identity id 15為:"+identity id 15);
if(fixPersonIDCode(identity id 15)。equalsIgnoreCase(identityId)) {
返回true
}否則{
返回false
}
} else if(identity id . length()= = 15){
嘗試{
long . parse long(identity id);
返回true
} catch (Exception ex) {
返回false
}
}否則{
返回false
}
} catch (Exception ex) {
返回false
}
}
/**
*
*判斷它是否為空字符串""
*/
public static boolean is empty(String sValue){
if (sValue == null)
返回true
返回sValue.trim()。等於(" ")?真:假;
}
公共靜態void main(String[] args) {
嘗試{
system . out . println(fixPersonIDCode(" 650103760113073 "));//身份證15到18。
system . out . println(isIdentityId(" 650103760113073 "));//No是有效的15位id號。
} catch(異常e) {
e . printstacktrace();
}
}
}