最近項目中需要給用戶增加身份證號字段,參考了幾位別人的實現。
特點:1、面向對象:把身份證號封裝為一個類,解析各個字段、驗證有效性都是對象上的實例方法。對比那種公開多個靜態方法的工具類的方式,我覺得這種面向對象的方式更自然一些。
2、不可變的。身份證號對象是不可變的,減少使用中的復雜性。
3、不是線程安全的。
001 |
import java.text.SimpleDateFormat; |
002 |
import java.util.Date; |
005 |
* 身份證號碼,可以解析身份證號碼的各個字段,以及驗證身份證號碼是否有效<br> |
006 |
* 身份證號碼構成:6位地址編碼+8位生日+3位順序碼+1位校驗碼 |
011 |
public class IDCard { |
015 |
private final String cardNumber; |
016 |
// 緩存身份證是否有效,因為驗證有效性使用頻繁且計算復雜 |
017 |
private Boolean cacheValidateResult = null ; |
018 |
// 緩存出生日期,因為出生日期使用頻繁且計算復雜 |
019 |
private Date cacheBirthDate = null ; |
021 |
public boolean validate() { |
022 |
if ( null == cacheValidateResult) { |
023 |
boolean result = true ; |
025 |
result = result && ( null != cardNumber); |
027 |
result = result && NEW_CARD_NUMBER_LENGTH == cardNumber.length(); |
029 |
for ( int i = 0 ; result && i < NEW_CARD_NUMBER_LENGTH - 1 ; i++) { |
030 |
char ch = cardNumber.charAt(i); |
031 |
result = result && ch >= '0' && ch <= '9' ; |
035 |
&& (calculateVerifyCode(cardNumber) == cardNumber |
036 |
.charAt(NEW_CARD_NUMBER_LENGTH - 1 )); |
037 |
// 出生日期不能晚于當前時間,并且不能早于1900年 |
039 |
Date birthDate = this .getBirthDate(); |
040 |
result = result && null != birthDate; |
041 |
result = result && birthDate.before( new Date()); |
042 |
result = result && birthDate.after(MINIMAL_BIRTH_DATE); |
044 |
* 出生日期中的年、月、日必須正確,比如月份范圍是[1,12],日期范圍是[1,31],還需要校驗閏年、大月、小月的情況時, |
047 |
String birthdayPart = this .getBirthDayPart(); |
048 |
String realBirthdayPart = this .createBirthDateParser().format( |
050 |
result = result && (birthdayPart.equals(realBirthdayPart)); |
051 |
} catch (Exception e) { |
054 |
// TODO 完整身份證號碼的省市縣區檢驗規則 |
055 |
cacheValidateResult = Boolean.valueOf(result); |
057 |
return cacheValidateResult; |
061 |
* 如果是15位身份證號碼,則自動轉換為18位 |
065 |
public IDCard(String cardNumber) { |
066 |
if ( null != cardNumber) { |
067 |
cardNumber = cardNumber.trim(); |
068 |
if (OLD_CARD_NUMBER_LENGTH == cardNumber.length()) { |
069 |
cardNumber = contertToNewCardNumber(cardNumber); |
072 |
this .cardNumber = cardNumber; |
075 |
public String getCardNumber() { |
079 |
public String getAddressCode() { |
081 |
return this .cardNumber.substring( 0 , 6 ); |
084 |
public Date getBirthDate() { |
085 |
if ( null == this .cacheBirthDate) { |
087 |
this .cacheBirthDate = this .createBirthDateParser().parse( |
088 |
this .getBirthDayPart()); |
089 |
} catch (Exception e) { |
090 |
throw new RuntimeException( "身份證的出生日期無效" ); |
093 |
return new Date( this .cacheBirthDate.getTime()); |
096 |
public boolean isMale() { |
097 |
return 1 == this .getGenderCode(); |
100 |
public boolean isFemal() { |
101 |
return false == this .isMale(); |
105 |
* 獲取身份證的第17位,奇數為男性,偶數為女性 |
109 |
private int getGenderCode() { |
111 |
char genderCode = this .cardNumber.charAt(NEW_CARD_NUMBER_LENGTH - 2 ); |
112 |
return ((( int ) (genderCode - '0' )) & 0x1 ); |
115 |
private String getBirthDayPart() { |
116 |
return this .cardNumber.substring( 6 , 14 ); |
119 |
private SimpleDateFormat createBirthDateParser() { |
120 |
return new SimpleDateFormat(BIRTH_DATE_FORMAT); |
123 |
private void checkIfValid() { |
124 |
if ( false == this .validate()) { |
125 |
throw new RuntimeException( "身份證號碼不正確!" ); |
130 |
private final static String BIRTH_DATE_FORMAT = "yyyyMMdd" ; |
131 |
// 身份證的最小出生日期,1900年1月1日 |
132 |
private final static Date MINIMAL_BIRTH_DATE = new Date(-2209017600000L); |
133 |
private final static int NEW_CARD_NUMBER_LENGTH = 18 ; |
134 |
private final static int OLD_CARD_NUMBER_LENGTH = 15 ; |
138 |
private final static char [] VERIFY_CODE = { '1' , '0' , 'X' , '9' , '8' , '7' , |
139 |
'6' , '5' , '4' , '3' , '2' }; |
141 |
* 18位身份證中,各個數字的生成校驗碼時的權值 |
143 |
private final static int [] VERIFY_CODE_WEIGHT = { 7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 , |
144 |
6 , 3 , 7 , 9 , 10 , 5 , 8 , 4 , 2 }; |
147 |
* <li>校驗碼(第十八位數):<br/> |
149 |
* <li>十七位數字本體碼加權求和公式 S = Sum(Ai * Wi), i = 0...16 ,先對前17位數字的權求和; |
150 |
* Ai:表示第i位置上的身份證號碼數字值 Wi:表示第i位置上的加權因子 Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 |
152 |
* <li>計算模 Y = mod(S, 11)</li> |
153 |
* <li>通過模得到對應的校驗碼 Y: 0 1 2 3 4 5 6 7 8 9 10 校驗碼: 1 0 X 9 8 7 6 5 4 3 2</li> |
159 |
private static char calculateVerifyCode(CharSequence cardNumber) { |
161 |
for ( int i = 0 ; i < NEW_CARD_NUMBER_LENGTH - 1 ; i++) { |
162 |
char ch = cardNumber.charAt(i); |
163 |
sum += (( int ) (ch - '0' )) * VERIFY_CODE_WEIGHT[i]; |
165 |
return VERIFY_CODE[sum % 11 ]; |
169 |
* 把15位身份證號碼轉換到18位身份證號碼<br> |
170 |
* 15位身份證號碼與18位身份證號碼的區別為:<br> |
171 |
* 1、15位身份證號碼中,"出生年份"字段是2位,轉換時需要補入"19",表示20世紀<br> |
172 |
* 2、15位身份證無最后一位校驗碼。18位身份證中,校驗碼根據根據前17位生成 |
177 |
private static String contertToNewCardNumber(String oldCardNumber) { |
178 |
StringBuilder buf = new StringBuilder(NEW_CARD_NUMBER_LENGTH); |
179 |
buf.append(oldCardNumber.substring( 0 , 6 )); |
181 |
buf.append(oldCardNumber.substring( 6 )); |
182 |
buf.append(IDCard.calculateVerifyCode(buf)); |
183 |
return buf.toString(); |