1、Java的身份证号码工具类1. /*2. *Copyright(C)2009-2010Yichuan,FuchunAllrightsreserved.3. *LicensedtotheApacheSoftwareFoundation(ASF)underoneormore4. *contributorlicenseagreements.SeetheNOTICEfiledistributedwith5. *thisworkforadditionalinformationregardingcopyrightownership.6. *TheASFlicensesthisfiletoYouundert
2、heApacheLicense,Version2.07. *(theLicense);youmaynotusethisfileexceptincompliancewith8. *theLicense.YoumayobtainacopyoftheLicenseat9. *http:/www.apache.org/licenses/LICENSE-2.010. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software11. *distributedundertheLicenseisdistributedonanASISBASIS,12.
3、*WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.13. *SeetheLicenseforthespecificlanguagegoverningpermissionsand14. *limitationsundertheLicense.15. *(#)IdcardUtils.javaDate:2010-06-1716. */17. packagemy.tools;18. 19. importjava.text.ParseException;20. importjava.text.SimpleDateFormat;2
4、1. importjava.util.Calendar;22. importjava.util.Date;23. importjava.util.HashMap;24. importjava.util.Map;25. 26. importmons.lang.StringUtils;27. 28. /*29. *身份证工具类30. *31. *authorJune32. *version1.0,2010-06-1733. */34. publicclassIdcardUtilsextendsStringUtils35. 36. /*中国公民身份证号码最小长度。*/37. publicstatic
5、finalintCHINA_ID_MIN_LENGTH=15;38. 39. /*中国公民身份证号码最大长度。*/40. publicstaticfinalintCHINA_ID_MAX_LENGTH=18;41. 42. /*省、直辖市代码表*/43. publicstaticfinalStringcityCode=44. 11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,45. 42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,46. 81,82,9147. ;48. 49. /*每位加权因子*/5
6、0. publicstaticfinalintpower=51. 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,252. ;53. 54. /*第18位校检码*/55. publicstaticfinalStringverifyCode=56. 1,0,X,9,8,7,6,5,4,3,257. ;58. /*最低年限*/59. publicstaticfinalintMIN=1930;60. publicstaticMapcityCodes=newHashMap();61. /*台湾身份首字母对应数字*/62. publicstaticMaptwFirstCode=new
7、HashMap();63. /*香港身份首字母对应数字*/64. publicstaticMaphkFirstCode=newHashMap();65. static66. cityCodes.put(11,北京);67. cityCodes.put(12,天津);68. cityCodes.put(13,河北);69. cityCodes.put(14,山西);70. cityCodes.put(15,内蒙古);71. cityCodes.put(21,辽宁);72. cityCodes.put(22,吉林);73. cityCodes.put(23,黑龙江);74. cityCodes.p
8、ut(31,上海);75. cityCodes.put(32,江苏);76. cityCodes.put(33,浙江);77. cityCodes.put(34,安徽);78. cityCodes.put(35,福建);79. cityCodes.put(36,江西);80. cityCodes.put(37,山东);81. cityCodes.put(41,河南);82. cityCodes.put(42,湖北);83. cityCodes.put(43,湖南);84. cityCodes.put(44,广东);85. cityCodes.put(45,广西);86. cityCodes.p
9、ut(46,海南);87. cityCodes.put(50,重庆);88. cityCodes.put(51,四川);89. cityCodes.put(52,贵州);90. cityCodes.put(53,云南);91. cityCodes.put(54,西藏);92. cityCodes.put(61,陕西);93. cityCodes.put(62,甘肃);94. cityCodes.put(63,青海);95. cityCodes.put(64,宁夏);96. cityCodes.put(65,新疆);97. cityCodes.put(71,台湾);98. cityCodes.p
10、ut(81,香港);99. cityCodes.put(82,澳门);100. cityCodes.put(91,国外);101. twFirstCode.put(A,10);102. twFirstCode.put(B,11);103. twFirstCode.put(C,12);104. twFirstCode.put(D,13);105. twFirstCode.put(E,14);106. twFirstCode.put(F,15);107. twFirstCode.put(G,16);108. twFirstCode.put(H,17);109. twFirstCode.put(J,
11、18);110. twFirstCode.put(K,19);111. twFirstCode.put(L,20);112. twFirstCode.put(M,21);113. twFirstCode.put(N,22);114. twFirstCode.put(P,23);115. twFirstCode.put(Q,24);116. twFirstCode.put(R,25);117. twFirstCode.put(S,26);118. twFirstCode.put(T,27);119. twFirstCode.put(U,28);120. twFirstCode.put(V,29)
12、;121. twFirstCode.put(X,30);122. twFirstCode.put(Y,31);123. twFirstCode.put(W,32);124. twFirstCode.put(Z,33);125. twFirstCode.put(I,34);126. twFirstCode.put(O,35);127. hkFirstCode.put(A,1);128. hkFirstCode.put(B,2);129. hkFirstCode.put(C,3);130. hkFirstCode.put(R,18);131. hkFirstCode.put(U,21);132.
13、hkFirstCode.put(Z,26);133. hkFirstCode.put(X,24);134. hkFirstCode.put(W,23);135. hkFirstCode.put(O,15);136. hkFirstCode.put(N,14);137. 138. 139. /*140. *将15位身份证号码转换为18位141. *142. *paramidCard143. *15位身份编码144. *return18位身份编码145. */146. publicstaticStringconver15CardTo18(StringidCard)147. StringidCard
14、18=;148. if(idCard.length()!=CHINA_ID_MIN_LENGTH)149. returnnull;150. 151. if(isNum(idCard)152. /获取出生年月日153. Stringbirthday=idCard.substring(6,12);154. DatebirthDate=null;155. try156. birthDate=newSimpleDateFormat(yyMMdd).parse(birthday);157. catch(ParseExceptione)158. e.printStackTrace();159. 160.
15、Calendarcal=Calendar.getInstance();161. if(birthDate!=null)162. cal.setTime(birthDate);163. /获取出生年(完全表现形式,如:2010)164. StringsYear=String.valueOf(cal.get(Calendar.YEAR);165. idCard18=idCard.substring(0,6)+sYear+idCard.substring(8);166. /转换字符数组167. charcArr=idCard18.toCharArray();168. if(cArr!=null)16
16、9. intiCard=converCharToInt(cArr);170. intiSum17=getPowerSum(iCard);171. /获取校验位172. StringsVal=getCheckCode18(iSum17);173. if(sVal.length()0)174. idCard18+=sVal;175. else176. returnnull;177. 178. 179. else180. returnnull;181. 182. returnidCard18;183. 184. 185. /*186. *验证身份证是否合法187. */188. publicstat
17、icbooleanvalidateCard(StringidCard)189. Stringcard=idCard.trim();190. if(validateIdCard18(card)191. returntrue;192. 193. if(validateIdCard15(card)194. returntrue;195. 196. Stringcardval=validateIdCard10(card);197. if(cardval!=null)198. if(cardval2.equals(true)199. returntrue;200. 201. 202. returnfal
18、se;203. 204. 205. /*206. *验证18位身份编码是否合法207. *208. *paramidCard身份编码209. *return是否合法210. */211. publicstaticbooleanvalidateIdCard18(StringidCard)212. booleanbTrue=false;213. if(idCard.length()=CHINA_ID_MAX_LENGTH)214. /前17位215. Stringcode17=idCard.substring(0,17);216. /第18位217. Stringcode18=idCard.sub
19、string(17,CHINA_ID_MAX_LENGTH);218. if(isNum(code17)219. charcArr=code17.toCharArray();220. if(cArr!=null)221. intiCard=converCharToInt(cArr);222. intiSum17=getPowerSum(iCard);223. /获取校验位224. Stringval=getCheckCode18(iSum17);225. if(val.length()0)226. if(val.equalsIgnoreCase(code18)227. bTrue=true;2
20、28. 229. 230. 231. 232. 233. returnbTrue;234. 235. 236. /*237. *验证15位身份编码是否合法238. *239. *paramidCard240. *身份编码241. *return是否合法242. */243. publicstaticbooleanvalidateIdCard15(StringidCard)244. if(idCard.length()!=CHINA_ID_MIN_LENGTH)245. returnfalse;246. 247. if(isNum(idCard)248. StringproCode=idCard.substring(0,2);249. if(cityCodes.get(proCode)=null)250.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1