知识大全 中华人民共和国公民身份证号码校验程序
Posted 公民
篇首语:大鹏一日同风起,扶摇直上九万里。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 中华人民共和国公民身份证号码校验程序相关的知识,希望对你有一定的参考价值。
/**
* roonten practice tools identitycard\\IdentityCard java
*
* Copyright (c) patulous function toolkit for java individual
* All rights reserved
*
* This toolkit all my items one is the last edition All the tools in the toolkit are defining
* some handy discreteness for me So I can doing items easily I hope you unfeignedly
* for use it
*/
package roonten practice tools identitycard;
import java util Calendar;
/**
* <div>
* The <code>IdentityCard</code> class is implements IIdentityCard
* interface In this interface there re some about checking an identity
* card need to using methods For example the year and the month and the day
* part this identity card must be right
* </div>
*
* @author Roon Ten
* @version
*/
public class IdentityCard
private char[] chrs;
private final String CHECK_STRING = ([ ] )[ Xx] ;
/**
* Create an object of this class by an scheduled identity card
* @param obj
*/
public IdentityCard(Object obj)
chrs = getChrs(obj);
/**
* Get a team for character by this identity card
* @param obj
* @return
*/
private char[] getChrs(Object obj)
return String valueOf(obj) toCharArray();
/**
* If this identity card is legal one return true
*/
public boolean isLegalityFormat()
return String valueOf(this chrs) matches(CHECK_STRING );
/**
* If the year this identity card is legal one return true
*/
public boolean isLegalityYear()
boolean isPass = false;
if (chrs != null && chrs length >= )
int year = Integer parseInt(chrs[ ] + + chrs[ ] + + chrs[ ]
+ + chrs[ ]);
int current = Calendar getInstance() get(Calendar YEAR);
isPass = year >= current && year <= current;
return isPass;
/**
* If the month this identity card is legal one return true
*/
public boolean isLegalityMonth()
boolean isPass = false;
if (chrs != null && chrs length >= )
int month = Integer parseInt(chrs[ ] + + chrs[ ]);
isPass = month > && month <= ;
return isPass;
/**
* If the day this identity card is legal one return true
*/
public boolean isLegalityDay()
boolean isPass = false;
if (chrs != null && chrs length >= )
int yy = Integer parseInt(chrs[ ] + + chrs[ ] + + chrs[ ]
+ + chrs[ ]);
int month = Integer parseInt(chrs[ ] + + chrs[ ]);
int day = Integer parseInt(chrs[ ] + + chrs[ ]);
Year year = new Year(yy);
isPass = year getMaxDay(month) >= day;
return isPass;
/**
* If the last character this identity card is legal one return true
*/
public boolean isLegalityEnd()
boolean isPass = false;
if (chrs != null && chrs length >= )
int num = (Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) *
+ Integer parseInt(chrs[ ] + ) * + Integer
parseInt(chrs[ ] + ) * ) % ;
switch (num)
case :
isPass = == chrs[ ];
break;
case :
isPass = == chrs[ ];
break;
case :
isPass = X == chrs[ ] || x == chrs[ ];
break;
case :
isPass = == chrs[ ];
break;
case :
isPass = == chrs[ ];
break;
case :
isPass = == chrs[ ];
break;
case :
isPass = == chrs[ ];
break;
case :
isPass = == chrs[ ];
break;
case :
isPass = == chrs[ ];
break;
case :
isPass = == chrs[ ];
break;
case :
isPass = == chrs[ ];
break;
return isPass;
/**
* Get the length this identity card
*/
public int getLength()
return this chrs length;
/**
* If it is a legal one
*/
public boolean isEffective()
boolean isValid = false;
isValid = isLegalityFormat();
if (isValid)
isValid = isLegalityYear();
if (isValid)
isValid = isLegalityMonth();
if (isValid)
isValid = isLegalityDay();
if (isValid)
isValid = isLegalityEnd();
return isValid;
/**
* The <code>Year</code> class use to get a month day of a year
* @author TenRoon
* @version
*/
class Year
/**
* Current year
*/
private int Year;
/**
* January days
*/
public static final int JAN_DAY = ;
/**
* February days of a year
* @param year
* @return
*/
public static final int FEB_DAY(int year)
int feb_Day = ;
if ((year % == && year % != ) || year % == )
feb_Day = ;
return feb_Day;
/**
* March days
*/
public static final int MAR_DAY = ;
/**
* April days
*/
public static final int APR_DAY = ;
/**
* May days
*/
public static final int MAY_DAY = ;
/**
* June days
*/
public static final int JUN_DAY = ;
/**
* July days
*/
public static final int JUL_DAY = ;
/**
* August days
*/
public static final int AUG_DAY = ;
/**
* September days
*/
public static final int SEP_DAY = ;
/**
* Octomber days
*/
public static final int OCT_DAY = ;
/**
* November days
*/
public static final int NOV_DAY = ;
/**
* December days
*/
public static final int DEC_DAY = ;
/**
* Create a object of a current year
* @param Year
*/
public Year(int Year)
this Year = Year;
/**
* Get the max day a month
* @param month
* @return
*/
public int getMaxDay(int month)
int maxDay = ;
switch (month)
case :
maxDay = JAN_DAY;
break;
case :
maxDay = FEB_DAY(this Year);
break;
case :
maxDay = MAR_DAY;
break;
case :
maxDay = APR_DAY;
break;
case :
maxDay = MAY_DAY;
break;
case :
maxDay = JUN_DAY;
break;
case :
maxDay = JUL_DAY;
break;
case :
maxDay = AUG_DAY;
break;
case :
maxDay = SEP_DAY;
break;
case :
maxDay = OCT_DAY;
break;
case :
maxDay = NOV_DAY;
break;
case :
maxDay = DEC_DAY;
break;
return maxDay;
cha138/Article/program/Java/hx/201311/25785
相关参考