Java的身份证号码工具类.docx
《Java的身份证号码工具类.docx》由会员分享,可在线阅读,更多相关《Java的身份证号码工具类.docx(22页珍藏版)》请在冰豆网上搜索。
![Java的身份证号码工具类.docx](https://file1.bdocx.com/fileroot1/2022-12/8/c319efcf-6d1a-4a83-bf03-0bffe702a2ea/c319efcf-6d1a-4a83-bf03-0bffe702a2ea1.gif)
Java的身份证号码工具类
1./**
2. * Copyright (C) 2009-2010 Yichuan, Fuchun All rights reserved.
3. * Licensed to the Apache Software Foundation (ASF) under one or more
4. * contributor license agreements. See the NOTICE file distributed with
5. * this work for additional information regarding copyright ownership.
6. * The ASF licenses this file to You under the Apache License, Version 2.0
7. * (the "License"); you may not use this file except in compliance with
8. * the License. You may obtain a copy of the License at
9. * http:
//www.apache.org/licenses/LICENSE-2.0
10. * Unless required by applicable law or agreed to in writing, software
11. * distributed under the License is distributed on an "AS IS" BASIS,
12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13. * See the License for the specific language governing permissions and
14. * limitations under the License.
15. * @(#) IdcardUtils.java Date:
2010-06-17
16. */
17.package my.tools;
18.
19.import java.text.ParseException;
20.import java.text.SimpleDateFormat;
21.import java.util.Calendar;
22.import java.util.Date;
23.import java.util.HashMap;
24.import java.util.Map;
25.
26.import mons.lang.StringUtils;
27.
28./**
29. * 身份证工具类
30. *
31. * @author June
32. * @version 1.0, 2010-06-17
33. */
34.public class IdcardUtils extends StringUtils {
35.
36. /** 中国公民身份证号码最小长度。
*/
37. public static final int CHINA_ID_MIN_LENGTH = 15;
38.
39. /** 中国公民身份证号码最大长度。
*/
40. public static final int CHINA_ID_MAX_LENGTH = 18;
41.
42. /** 省、直辖市代码表 */
43. public static final String cityCode[] = {
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", "91"
47. };
48.
49. /** 每位加权因子 */
50. public static final int power[] = {
51. 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2
52. };
53.
54. /** 第18位校检码 */
55. public static final String verifyCode[] = {
56. "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"
57. };
58. /** 最低年限 */
59. public static final int MIN = 1930;
60. public static Map cityCodes = new HashMap();
61. /** 台湾身份首字母对应数字 */
62. public static Map twFirstCode = new HashMap();
63. /** 香港身份首字母对应数字 */
64. public static Map hkFirstCode = new HashMap();
65. static {
66. 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.put("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.put("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.put("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", 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);
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. 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. * @param idCard
143. * 15位身份编码
144. * @return 18位身份编码
145. */
146. public static String conver15CardTo18(String idCard) {
147. String idCard18 = "";
148. if (idCard.length() !
= CHINA_ID_MIN_LENGTH) {
149. return null;
150. }
151. if (isNum(idCard)) {
152. // 获取出生年月日
153. String birthday = idCard.substring(6, 12);
154. Date birthDate = null;
155. try {
156. birthDate = new SimpleDateFormat("yyMMdd").parse(birthday);
157. } catch (ParseException e) {
158. e.printStackTrace();
159. }
160. Calendar cal = Calendar.getInstance();
161. if (birthDate !
= null)
162. cal.setTime(birthDate);
163. // 获取出生年(完全表现形式,如:
2010)
164. String sYear = String.valueOf(cal.get(Calendar.YEAR));
165. idCard18 = idCard.substring(0, 6) + sYear + idCard.substring(8);
166. // 转换字符数组
167. char[] cArr = idCard18.toCharArray();
168. if (cArr !
= null) {
169. int[] iCard = converCharToInt(cArr);
170. int iSum17 = getPowerSum(iCard);
171. // 获取校验位
172. String sVal = getCheckCode18(iSum17);
173. if (sVal.length() > 0) {
174. idCard18 += sVal;
175. } else {
176. return null;
177. }
178. }
179. } else {
180. return null;
181. }
182. return idCard18;
183. }
184.
185. /**
186. * 验证身份证是否合法
187. */
188. public static boolean validateCard(String idCard) {
189. String card = idCard.trim();
190. if (validateIdCard18(card)) {
191. return true;
192. }
193. if (validateIdCard15(card)) {
194. return true;
195. }
196. String[] cardval = validateIdCard10(card);
197. if (cardval !
= null) {
198. if (cardval[2].equals("true")) {
199. return true;
200. }
201. }
202. return false;
203. }
204.
205. /**
206. * 验证18位身份编码是否合法
207. *
208. * @param idCard 身份编码
209. * @return 是否合法
210. */
211. public static boolean validateIdCard18(String idCard) {
212. boolean bTrue = false;
213. if (idCard.length() == CHINA_ID_MAX_LENGTH) {
214. // 前17位
215. String code17 = idCard.substring(0, 17);
216. // 第18位
217. String code18 = idCard.substring(17, CHINA_ID_MAX_LENGTH);
218. if (isNum(code17)) {
219. char[] cArr = code17.toCharArray();
220. if (cArr !
= null) {
221. int[] iCard = converCharToInt(cArr);
222. int iSum17 = getPowerSum(iCard);
223. // 获取校验位
224. String val = getCheckCode18(iSum17);
225. if (val.length() > 0) {
226. if (val.equalsIgnoreCase(code18)) {
227. bTrue = true;
228. }
229. }
230. }
231. }
232. }
233. return bTrue;
234. }
235.
236. /**
237. * 验证15位身份编码是否合法
238. *
239. * @param idCard
240. * 身份编码
241. * @return 是否合法
242. */
243. public static boolean validateIdCard15(String idCard) {
244. if (idCard.length() !
= CHINA_ID_MIN_LENGTH) {
245. return false;
246. }
247. if (isNum(idCard)) {
248. String proCode = idCard.substring(0, 2);
249. if (cityCodes.get(proCode) == null) {
250.