提交 | 用户 | 时间
|
58d006
|
1 |
|
A |
2 |
package com.mandi.common; |
|
3 |
|
|
4 |
import java.text.ParseException; |
|
5 |
import java.text.SimpleDateFormat; |
|
6 |
import java.util.Calendar; |
|
7 |
import java.util.Date; |
|
8 |
import java.util.HashMap; |
|
9 |
import java.util.Map; |
|
10 |
|
|
11 |
/** |
|
12 |
* 身份证工具类 |
|
13 |
* |
|
14 |
* @author June |
|
15 |
* @version 1.0, 2010-06-17 |
|
16 |
*/ |
|
17 |
public class IdcardUtils { |
|
18 |
|
|
19 |
/** 中国公民身份证号码最小长度。 */ |
|
20 |
public static final int CHINA_ID_MIN_LENGTH = 15; |
|
21 |
|
|
22 |
/** 中国公民身份证号码最大长度。 */ |
|
23 |
public static final int CHINA_ID_MAX_LENGTH = 18; |
|
24 |
|
|
25 |
/** 省、直辖市代码表 */ |
|
26 |
public static final String cityCode[] = { |
|
27 |
"11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", |
|
28 |
"42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", |
|
29 |
"81", "82", "91" |
|
30 |
}; |
|
31 |
|
|
32 |
/** 每位加权因子 */ |
|
33 |
public static final int power[] = { |
|
34 |
7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 |
|
35 |
}; |
|
36 |
|
|
37 |
/** 第18位校检码 */ |
|
38 |
public static final String verifyCode[] = { |
|
39 |
"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" |
|
40 |
}; |
|
41 |
/** 最低年限 */ |
|
42 |
public static final int MIN = 1930; |
|
43 |
public static Map<String, String> cityCodes = new HashMap<String, String>(); |
|
44 |
/** 台湾身份首字母对应数字 */ |
|
45 |
public static Map<String, Integer> twFirstCode = new HashMap<String, Integer>(); |
|
46 |
/** 香港身份首字母对应数字 */ |
|
47 |
public static Map<String, Integer> hkFirstCode = new HashMap<String, Integer>(); |
|
48 |
static { |
|
49 |
cityCodes.put("11", "北京"); |
|
50 |
cityCodes.put("12", "天津"); |
|
51 |
cityCodes.put("13", "河北"); |
|
52 |
cityCodes.put("14", "山西"); |
|
53 |
cityCodes.put("15", "内蒙古"); |
|
54 |
cityCodes.put("21", "辽宁"); |
|
55 |
cityCodes.put("22", "吉林"); |
|
56 |
cityCodes.put("23", "黑龙江"); |
|
57 |
cityCodes.put("31", "上海"); |
|
58 |
cityCodes.put("32", "江苏"); |
|
59 |
cityCodes.put("33", "浙江"); |
|
60 |
cityCodes.put("34", "安徽"); |
|
61 |
cityCodes.put("35", "福建"); |
|
62 |
cityCodes.put("36", "江西"); |
|
63 |
cityCodes.put("37", "山东"); |
|
64 |
cityCodes.put("41", "河南"); |
|
65 |
cityCodes.put("42", "湖北"); |
|
66 |
cityCodes.put("43", "湖南"); |
|
67 |
cityCodes.put("44", "广东"); |
|
68 |
cityCodes.put("45", "广西"); |
|
69 |
cityCodes.put("46", "海南"); |
|
70 |
cityCodes.put("50", "重庆"); |
|
71 |
cityCodes.put("51", "四川"); |
|
72 |
cityCodes.put("52", "贵州"); |
|
73 |
cityCodes.put("53", "云南"); |
|
74 |
cityCodes.put("54", "西藏"); |
|
75 |
cityCodes.put("61", "陕西"); |
|
76 |
cityCodes.put("62", "甘肃"); |
|
77 |
cityCodes.put("63", "青海"); |
|
78 |
cityCodes.put("64", "宁夏"); |
|
79 |
cityCodes.put("65", "新疆"); |
|
80 |
cityCodes.put("71", "台湾"); |
|
81 |
cityCodes.put("81", "香港"); |
|
82 |
cityCodes.put("82", "澳门"); |
|
83 |
cityCodes.put("91", "国外"); |
|
84 |
twFirstCode.put("A", 10); |
|
85 |
twFirstCode.put("B", 11); |
|
86 |
twFirstCode.put("C", 12); |
|
87 |
twFirstCode.put("D", 13); |
|
88 |
twFirstCode.put("E", 14); |
|
89 |
twFirstCode.put("F", 15); |
|
90 |
twFirstCode.put("G", 16); |
|
91 |
twFirstCode.put("H", 17); |
|
92 |
twFirstCode.put("J", 18); |
|
93 |
twFirstCode.put("K", 19); |
|
94 |
twFirstCode.put("L", 20); |
|
95 |
twFirstCode.put("M", 21); |
|
96 |
twFirstCode.put("N", 22); |
|
97 |
twFirstCode.put("P", 23); |
|
98 |
twFirstCode.put("Q", 24); |
|
99 |
twFirstCode.put("R", 25); |
|
100 |
twFirstCode.put("S", 26); |
|
101 |
twFirstCode.put("T", 27); |
|
102 |
twFirstCode.put("U", 28); |
|
103 |
twFirstCode.put("V", 29); |
|
104 |
twFirstCode.put("X", 30); |
|
105 |
twFirstCode.put("Y", 31); |
|
106 |
twFirstCode.put("W", 32); |
|
107 |
twFirstCode.put("Z", 33); |
|
108 |
twFirstCode.put("I", 34); |
|
109 |
twFirstCode.put("O", 35); |
|
110 |
hkFirstCode.put("A", 1); |
|
111 |
hkFirstCode.put("B", 2); |
|
112 |
hkFirstCode.put("C", 3); |
|
113 |
hkFirstCode.put("R", 18); |
|
114 |
hkFirstCode.put("U", 21); |
|
115 |
hkFirstCode.put("Z", 26); |
|
116 |
hkFirstCode.put("X", 24); |
|
117 |
hkFirstCode.put("W", 23); |
|
118 |
hkFirstCode.put("O", 15); |
|
119 |
hkFirstCode.put("N", 14); |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* 将15位身份证号码转换为18位 |
|
124 |
* |
|
125 |
* @param idCard |
|
126 |
* 15位身份编码 |
|
127 |
* @return 18位身份编码 |
|
128 |
*/ |
|
129 |
public static String conver15CardTo18(String idCard) { |
|
130 |
String idCard18 = ""; |
|
131 |
if (idCard.length() != CHINA_ID_MIN_LENGTH) { |
|
132 |
return null; |
|
133 |
} |
|
134 |
if (isNum(idCard)) { |
|
135 |
// 获取出生年月日 |
|
136 |
String birthday = idCard.substring(6, 12); |
|
137 |
Date birthDate = null; |
|
138 |
try { |
|
139 |
birthDate = new SimpleDateFormat("yyMMdd").parse(birthday); |
|
140 |
} catch (ParseException e) { |
|
141 |
e.printStackTrace(); |
|
142 |
} |
|
143 |
Calendar cal = Calendar.getInstance(); |
|
144 |
if (birthDate != null) |
|
145 |
cal.setTime(birthDate); |
|
146 |
// 获取出生年(完全表现形式,如:2010) |
|
147 |
String sYear = String.valueOf(cal.get(Calendar.YEAR)); |
|
148 |
idCard18 = idCard.substring(0, 6) + sYear + idCard.substring(8); |
|
149 |
// 转换字符数组 |
|
150 |
char[] cArr = idCard18.toCharArray(); |
|
151 |
if (cArr != null) { |
|
152 |
int[] iCard = converCharToInt(cArr); |
|
153 |
int iSum17 = getPowerSum(iCard); |
|
154 |
// 获取校验位 |
|
155 |
String sVal = getCheckCode18(iSum17); |
|
156 |
if (sVal.length() > 0) { |
|
157 |
idCard18 += sVal; |
|
158 |
} else { |
|
159 |
return null; |
|
160 |
} |
|
161 |
} |
|
162 |
} else { |
|
163 |
return null; |
|
164 |
} |
|
165 |
return idCard18; |
|
166 |
} |
|
167 |
|
|
168 |
/** |
|
169 |
* 验证身份证是否合法 |
|
170 |
*/ |
|
171 |
public static boolean validateCard(String idCard) { |
|
172 |
String card = idCard.trim(); |
|
173 |
if (validateIdCard18(card)) { |
|
174 |
return true; |
|
175 |
} |
|
176 |
if (validateIdCard15(card)) { |
|
177 |
return true; |
|
178 |
} |
|
179 |
String[] cardval = validateIdCard10(card); |
|
180 |
if (cardval != null) { |
|
181 |
if (cardval[2].equals("true")) { |
|
182 |
return true; |
|
183 |
} |
|
184 |
} |
|
185 |
return false; |
|
186 |
} |
|
187 |
|
|
188 |
/** |
|
189 |
* 验证18位身份编码是否合法 |
|
190 |
* |
|
191 |
* @param idCard 身份编码 |
|
192 |
* @return 是否合法 |
|
193 |
*/ |
|
194 |
public static boolean validateIdCard18(String idCard) { |
|
195 |
boolean bTrue = false; |
|
196 |
if (idCard.length() == CHINA_ID_MAX_LENGTH) { |
|
197 |
// 前17位 |
|
198 |
String code17 = idCard.substring(0, 17); |
|
199 |
// 第18位 |
|
200 |
String code18 = idCard.substring(17, CHINA_ID_MAX_LENGTH); |
|
201 |
if (isNum(code17)) { |
|
202 |
char[] cArr = code17.toCharArray(); |
|
203 |
if (cArr != null) { |
|
204 |
int[] iCard = converCharToInt(cArr); |
|
205 |
int iSum17 = getPowerSum(iCard); |
|
206 |
// 获取校验位 |
|
207 |
String val = getCheckCode18(iSum17); |
|
208 |
if (val.length() > 0) { |
|
209 |
if (val.equalsIgnoreCase(code18)) { |
|
210 |
bTrue = true; |
|
211 |
} |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
return bTrue; |
|
217 |
} |
|
218 |
|
|
219 |
/** |
|
220 |
* 验证15位身份编码是否合法 |
|
221 |
* |
|
222 |
* @param idCard |
|
223 |
* 身份编码 |
|
224 |
* @return 是否合法 |
|
225 |
*/ |
|
226 |
public static boolean validateIdCard15(String idCard) { |
|
227 |
if (idCard.length() != CHINA_ID_MIN_LENGTH) { |
|
228 |
return false; |
|
229 |
} |
|
230 |
if (isNum(idCard)) { |
|
231 |
String proCode = idCard.substring(0, 2); |
|
232 |
if (cityCodes.get(proCode) == null) { |
|
233 |
return false; |
|
234 |
} |
|
235 |
String birthCode = idCard.substring(6, 12); |
|
236 |
Date birthDate = null; |
|
237 |
try { |
|
238 |
birthDate = new SimpleDateFormat("yy").parse(birthCode.substring(0, 2)); |
|
239 |
} catch (ParseException e) { |
|
240 |
e.printStackTrace(); |
|
241 |
} |
|
242 |
Calendar cal = Calendar.getInstance(); |
|
243 |
if (birthDate != null) |
|
244 |
cal.setTime(birthDate); |
|
245 |
if (!valiDate(cal.get(Calendar.YEAR), Integer.valueOf(birthCode.substring(2, 4)), |
|
246 |
Integer.valueOf(birthCode.substring(4, 6)))) { |
|
247 |
return false; |
|
248 |
} |
|
249 |
} else { |
|
250 |
return false; |
|
251 |
} |
|
252 |
return true; |
|
253 |
} |
|
254 |
|
|
255 |
/** |
|
256 |
* 验证10位身份编码是否合法 |
|
257 |
* |
|
258 |
* @param idCard 身份编码 |
|
259 |
* @return 身份证信息数组 |
|
260 |
* <p> |
|
261 |
* [0] - 台湾、澳门、香港 [1] - 性别(男M,女F,未知N) [2] - 是否合法(合法true,不合法false) |
|
262 |
* 若不是身份证件号码则返回null |
|
263 |
* </p> |
|
264 |
*/ |
|
265 |
public static String[] validateIdCard10(String idCard) { |
|
266 |
String[] info = new String[3]; |
|
267 |
String card = idCard.replaceAll("[\\(|\\)]", ""); |
|
268 |
if (card.length() != 8 && card.length() != 9 && idCard.length() != 10) { |
|
269 |
return null; |
|
270 |
} |
|
271 |
if (idCard.matches("^[a-zA-Z][0-9]{9}$")) { // 台湾 |
|
272 |
info[0] = "台湾"; |
|
273 |
System.out.println("11111"); |
|
274 |
String char2 = idCard.substring(1, 2); |
|
275 |
if (char2.equals("1")) { |
|
276 |
info[1] = "M"; |
|
277 |
System.out.println("MMMMMMM"); |
|
278 |
} else if (char2.equals("2")) { |
|
279 |
info[1] = "F"; |
|
280 |
System.out.println("FFFFFFF"); |
|
281 |
} else { |
|
282 |
info[1] = "N"; |
|
283 |
info[2] = "false"; |
|
284 |
System.out.println("NNNN"); |
|
285 |
return info; |
|
286 |
} |
|
287 |
info[2] = validateTWCard(idCard) ? "true" : "false"; |
|
288 |
} else if (idCard.matches("^[1|5|7][0-9]{6}\\(?[0-9A-Z]\\)?$")) { // 澳门 |
|
289 |
info[0] = "澳门"; |
|
290 |
info[1] = "N"; |
|
291 |
// TODO |
|
292 |
} else if (idCard.matches("^[A-Z]{1,2}[0-9]{6}\\(?[0-9A]\\)?$")) { // 香港 |
|
293 |
info[0] = "香港"; |
|
294 |
info[1] = "N"; |
|
295 |
info[2] = validateHKCard(idCard) ? "true" : "false"; |
|
296 |
} else { |
|
297 |
return null; |
|
298 |
} |
|
299 |
return info; |
|
300 |
} |
|
301 |
|
|
302 |
/** |
|
303 |
* 验证台湾身份证号码 |
|
304 |
* |
|
305 |
* @param idCard |
|
306 |
* 身份证号码 |
|
307 |
* @return 验证码是否符合 |
|
308 |
*/ |
|
309 |
public static boolean validateTWCard(String idCard) { |
|
310 |
String start = idCard.substring(0, 1); |
|
311 |
String mid = idCard.substring(1, 9); |
|
312 |
String end = idCard.substring(9, 10); |
|
313 |
Integer iStart = twFirstCode.get(start); |
|
314 |
Integer sum = iStart / 10 + (iStart % 10) * 9; |
|
315 |
char[] chars = mid.toCharArray(); |
|
316 |
Integer iflag = 8; |
|
317 |
for (char c : chars) { |
|
318 |
sum = sum + Integer.valueOf(c + "") * iflag; |
|
319 |
iflag--; |
|
320 |
} |
|
321 |
return (sum % 10 == 0 ? 0 : (10 - sum % 10)) == Integer.valueOf(end) ? true : false; |
|
322 |
} |
|
323 |
|
|
324 |
/** |
|
325 |
* 验证香港身份证号码(存在Bug,部份特殊身份证无法检查) |
|
326 |
* <p> |
|
327 |
* 身份证前2位为英文字符,如果只出现一个英文字符则表示第一位是空格,对应数字58 前2位英文字符A-Z分别对应数字10-35 |
|
328 |
* 最后一位校验码为0-9的数字加上字符"A","A"代表10 |
|
329 |
* </p> |
|
330 |
* <p> |
|
331 |
* 将身份证号码全部转换为数字,分别对应乘9-1相加的总和,整除11则证件号码有效 |
|
332 |
* </p> |
|
333 |
* |
|
334 |
* @param idCard 身份证号码 |
|
335 |
* @return 验证码是否符合 |
|
336 |
*/ |
|
337 |
public static boolean validateHKCard(String idCard) { |
|
338 |
String card = idCard.replaceAll("[\\(|\\)]", ""); |
|
339 |
Integer sum = 0; |
|
340 |
if (card.length() == 9) { |
|
341 |
sum = (Integer.valueOf(card.substring(0, 1).toUpperCase().toCharArray()[0]) - 55) * 9 |
|
342 |
+ (Integer.valueOf(card.substring(1, 2).toUpperCase().toCharArray()[0]) - 55) * 8; |
|
343 |
card = card.substring(1, 9); |
|
344 |
} else { |
|
345 |
sum = 522 + (Integer.valueOf(card.substring(0, 1).toUpperCase().toCharArray()[0]) - 55) * 8; |
|
346 |
} |
|
347 |
String mid = card.substring(1, 7); |
|
348 |
String end = card.substring(7, 8); |
|
349 |
char[] chars = mid.toCharArray(); |
|
350 |
Integer iflag = 7; |
|
351 |
for (char c : chars) { |
|
352 |
sum = sum + Integer.valueOf(c + "") * iflag; |
|
353 |
iflag--; |
|
354 |
} |
|
355 |
if (end.toUpperCase().equals("A")) { |
|
356 |
sum = sum + 10; |
|
357 |
} else { |
|
358 |
sum = sum + Integer.valueOf(end); |
|
359 |
} |
|
360 |
return (sum % 11 == 0) ? true : false; |
|
361 |
} |
|
362 |
|
|
363 |
/** |
|
364 |
* 将字符数组转换成数字数组 |
|
365 |
* |
|
366 |
* @param ca |
|
367 |
* 字符数组 |
|
368 |
* @return 数字数组 |
|
369 |
*/ |
|
370 |
public static int[] converCharToInt(char[] ca) { |
|
371 |
int len = ca.length; |
|
372 |
int[] iArr = new int[len]; |
|
373 |
try { |
|
374 |
for (int i = 0; i < len; i++) { |
|
375 |
iArr[i] = Integer.parseInt(String.valueOf(ca[i])); |
|
376 |
} |
|
377 |
} catch (NumberFormatException e) { |
|
378 |
e.printStackTrace(); |
|
379 |
} |
|
380 |
return iArr; |
|
381 |
} |
|
382 |
|
|
383 |
/** |
|
384 |
* 将身份证的每位和对应位的加权因子相乘之后,再得到和值 |
|
385 |
* |
|
386 |
* @param iArr |
|
387 |
* @return 身份证编码。 |
|
388 |
*/ |
|
389 |
public static int getPowerSum(int[] iArr) { |
|
390 |
int iSum = 0; |
|
391 |
if (power.length == iArr.length) { |
|
392 |
for (int i = 0; i < iArr.length; i++) { |
|
393 |
for (int j = 0; j < power.length; j++) { |
|
394 |
if (i == j) { |
|
395 |
iSum = iSum + iArr[i] * power[j]; |
|
396 |
} |
|
397 |
} |
|
398 |
} |
|
399 |
} |
|
400 |
return iSum; |
|
401 |
} |
|
402 |
|
|
403 |
/** |
|
404 |
* 将power和值与11取模获得余数进行校验码判断 |
|
405 |
* |
|
406 |
* @param iSum |
|
407 |
* @return 校验位 |
|
408 |
*/ |
|
409 |
public static String getCheckCode18(int iSum) { |
|
410 |
String sCode = ""; |
|
411 |
switch (iSum % 11) { |
|
412 |
case 10: |
|
413 |
sCode = "2"; |
|
414 |
break; |
|
415 |
case 9: |
|
416 |
sCode = "3"; |
|
417 |
break; |
|
418 |
case 8: |
|
419 |
sCode = "4"; |
|
420 |
break; |
|
421 |
case 7: |
|
422 |
sCode = "5"; |
|
423 |
break; |
|
424 |
case 6: |
|
425 |
sCode = "6"; |
|
426 |
break; |
|
427 |
case 5: |
|
428 |
sCode = "7"; |
|
429 |
break; |
|
430 |
case 4: |
|
431 |
sCode = "8"; |
|
432 |
break; |
|
433 |
case 3: |
|
434 |
sCode = "9"; |
|
435 |
break; |
|
436 |
case 2: |
|
437 |
sCode = "x"; |
|
438 |
break; |
|
439 |
case 1: |
|
440 |
sCode = "0"; |
|
441 |
break; |
|
442 |
case 0: |
|
443 |
sCode = "1"; |
|
444 |
break; |
|
445 |
} |
|
446 |
return sCode; |
|
447 |
} |
|
448 |
|
|
449 |
/** |
|
450 |
* 根据身份编号获取年龄 |
|
451 |
* |
|
452 |
* @param idCard |
|
453 |
* 身份编号 |
|
454 |
* @return 年龄 |
|
455 |
*/ |
|
456 |
public static int getAgeByIdCard(String idCard) { |
|
457 |
int iAge = 0; |
|
458 |
if (idCard.length() == CHINA_ID_MIN_LENGTH) { |
|
459 |
idCard = conver15CardTo18(idCard); |
|
460 |
} |
|
461 |
String year = idCard.substring(6, 10); |
|
462 |
Calendar cal = Calendar.getInstance(); |
|
463 |
int iCurrYear = cal.get(Calendar.YEAR); |
|
464 |
iAge = iCurrYear - Integer.valueOf(year); |
|
465 |
return iAge; |
|
466 |
} |
|
467 |
|
|
468 |
/** |
|
469 |
* 根据身份编号获取生日 |
|
470 |
* |
|
471 |
* @param idCard 身份编号 |
|
472 |
* @return 生日(yyyyMMdd) |
|
473 |
*/ |
|
474 |
public static String getBirthByIdCard(String idCard) { |
|
475 |
Integer len = idCard.length(); |
|
476 |
if (len < CHINA_ID_MIN_LENGTH) { |
|
477 |
return null; |
|
478 |
} else if (len == CHINA_ID_MIN_LENGTH) { |
|
479 |
idCard = conver15CardTo18(idCard); |
|
480 |
} |
|
481 |
return idCard.substring(6, 14); |
|
482 |
} |
|
483 |
|
|
484 |
/** |
|
485 |
* 根据身份编号获取生日年 |
|
486 |
* |
|
487 |
* @param idCard 身份编号 |
|
488 |
* @return 生日(yyyy) |
|
489 |
*/ |
|
490 |
public static Short getYearByIdCard(String idCard) { |
|
491 |
Integer len = idCard.length(); |
|
492 |
if (len < CHINA_ID_MIN_LENGTH) { |
|
493 |
return null; |
|
494 |
} else if (len == CHINA_ID_MIN_LENGTH) { |
|
495 |
idCard = conver15CardTo18(idCard); |
|
496 |
} |
|
497 |
return Short.valueOf(idCard.substring(6, 10)); |
|
498 |
} |
|
499 |
|
|
500 |
/** |
|
501 |
* 根据身份编号获取生日月 |
|
502 |
* |
|
503 |
* @param idCard |
|
504 |
* 身份编号 |
|
505 |
* @return 生日(MM) |
|
506 |
*/ |
|
507 |
public static Short getMonthByIdCard(String idCard) { |
|
508 |
Integer len = idCard.length(); |
|
509 |
if (len < CHINA_ID_MIN_LENGTH) { |
|
510 |
return null; |
|
511 |
} else if (len == CHINA_ID_MIN_LENGTH) { |
|
512 |
idCard = conver15CardTo18(idCard); |
|
513 |
} |
|
514 |
return Short.valueOf(idCard.substring(10, 12)); |
|
515 |
} |
|
516 |
|
|
517 |
/** |
|
518 |
* 根据身份编号获取生日天 |
|
519 |
* |
|
520 |
* @param idCard |
|
521 |
* 身份编号 |
|
522 |
* @return 生日(dd) |
|
523 |
*/ |
|
524 |
public static Short getDateByIdCard(String idCard) { |
|
525 |
Integer len = idCard.length(); |
|
526 |
if (len < CHINA_ID_MIN_LENGTH) { |
|
527 |
return null; |
|
528 |
} else if (len == CHINA_ID_MIN_LENGTH) { |
|
529 |
idCard = conver15CardTo18(idCard); |
|
530 |
} |
|
531 |
return Short.valueOf(idCard.substring(12, 14)); |
|
532 |
} |
|
533 |
|
|
534 |
/** |
|
535 |
* 根据身份编号获取性别 |
|
536 |
* |
|
537 |
* @param idCard 身份编号 |
|
538 |
* @return 性别(M-男,F-女,N-未知) |
|
539 |
*/ |
|
540 |
public static String getGenderByIdCard(String idCard) { |
|
541 |
String sGender = "N"; |
|
542 |
if (idCard.length() == CHINA_ID_MIN_LENGTH) { |
|
543 |
idCard = conver15CardTo18(idCard); |
|
544 |
} |
|
545 |
String sCardNum = idCard.substring(16, 17); |
|
546 |
if (Integer.parseInt(sCardNum) % 2 != 0) { |
|
547 |
sGender = "M"; |
|
548 |
} else { |
|
549 |
sGender = "F"; |
|
550 |
} |
|
551 |
return sGender; |
|
552 |
} |
|
553 |
|
|
554 |
/** |
|
555 |
* 根据身份编号获取户籍省份 |
|
556 |
* |
|
557 |
* @param idCard 身份编码 |
|
558 |
* @return 省级编码。 |
|
559 |
*/ |
|
560 |
public static String getProvinceByIdCard(String idCard) { |
|
561 |
int len = idCard.length(); |
|
562 |
String sProvince = null; |
|
563 |
String sProvinNum = ""; |
|
564 |
if (len == CHINA_ID_MIN_LENGTH || len == CHINA_ID_MAX_LENGTH) { |
|
565 |
sProvinNum = idCard.substring(0, 2); |
|
566 |
} |
|
567 |
sProvince = cityCodes.get(sProvinNum); |
|
568 |
return sProvince; |
|
569 |
} |
|
570 |
|
|
571 |
/** |
|
572 |
* 数字验证 |
|
573 |
* |
|
574 |
* @param val |
|
575 |
* @return 提取的数字。 |
|
576 |
*/ |
|
577 |
public static boolean isNum(String val) { |
|
578 |
return val == null || "".equals(val) ? false : val.matches("^[0-9]*$"); |
|
579 |
} |
|
580 |
|
|
581 |
/** |
|
582 |
* 验证小于当前日期 是否有效 |
|
583 |
* |
|
584 |
* @param iYear |
|
585 |
* 待验证日期(年) |
|
586 |
* @param iMonth |
|
587 |
* 待验证日期(月 1-12) |
|
588 |
* @param iDate |
|
589 |
* 待验证日期(日) |
|
590 |
* @return 是否有效 |
|
591 |
*/ |
|
592 |
public static boolean valiDate(int iYear, int iMonth, int iDate) { |
|
593 |
Calendar cal = Calendar.getInstance(); |
|
594 |
int year = cal.get(Calendar.YEAR); |
|
595 |
int datePerMonth; |
|
596 |
if (iYear < MIN || iYear >= year) { |
|
597 |
return false; |
|
598 |
} |
|
599 |
if (iMonth < 1 || iMonth > 12) { |
|
600 |
return false; |
|
601 |
} |
|
602 |
switch (iMonth) { |
|
603 |
case 4: |
|
604 |
case 6: |
|
605 |
case 9: |
|
606 |
case 11: |
|
607 |
datePerMonth = 30; |
|
608 |
break; |
|
609 |
case 2: |
|
610 |
boolean dm = ((iYear % 4 == 0 && iYear % 100 != 0) || (iYear % 400 == 0)) |
|
611 |
&& (iYear > MIN && iYear < year); |
|
612 |
datePerMonth = dm ? 29 : 28; |
|
613 |
break; |
|
614 |
default: |
|
615 |
datePerMonth = 31; |
|
616 |
} |
|
617 |
return (iDate >= 1) && (iDate <= datePerMonth); |
|
618 |
} |
|
619 |
} |