CommonUtil.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.ydtech.modules.ins.utils;
  2. import java.util.Calendar;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. public class CommonUtil {
  6. final static Map<Integer, String> zoneNum = new HashMap<Integer, String>();
  7. static {
  8. zoneNum.put(11, "北京");
  9. zoneNum.put(12, "天津");
  10. zoneNum.put(13, "河北");
  11. zoneNum.put(14, "山西");
  12. zoneNum.put(15, "内蒙古");
  13. zoneNum.put(21, "辽宁");
  14. zoneNum.put(22, "吉林");
  15. zoneNum.put(23, "黑龙江");
  16. zoneNum.put(31, "上海");
  17. zoneNum.put(32, "江苏");
  18. zoneNum.put(33, "浙江");
  19. zoneNum.put(34, "安徽");
  20. zoneNum.put(35, "福建");
  21. zoneNum.put(36, "江西");
  22. zoneNum.put(37, "山东");
  23. zoneNum.put(41, "河南");
  24. zoneNum.put(42, "湖北");
  25. zoneNum.put(43, "湖南");
  26. zoneNum.put(44, "广东");
  27. zoneNum.put(45, "广西");
  28. zoneNum.put(46, "海南");
  29. zoneNum.put(50, "重庆");
  30. zoneNum.put(51, "四川");
  31. zoneNum.put(52, "贵州");
  32. zoneNum.put(53, "云南");
  33. zoneNum.put(54, "西藏");
  34. zoneNum.put(61, "陕西");
  35. zoneNum.put(62, "甘肃");
  36. zoneNum.put(63, "青海");
  37. zoneNum.put(64, "宁夏");
  38. zoneNum.put(65, "新疆");
  39. zoneNum.put(71, "台湾");
  40. zoneNum.put(81, "香港");
  41. zoneNum.put(82, "澳门");
  42. zoneNum.put(91, "外国");
  43. }
  44. final static int[] PARITYBIT = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
  45. final static int[] POWER_LIST = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10,
  46. 5, 8, 4, 2};
  47. /**
  48. * 身份证验证
  49. *
  50. * @param certNo 号码内容
  51. * @return 是否有效 null和"" 都是false
  52. */
  53. public static boolean isIDCard(String certNo) {
  54. if (certNo == null || (certNo.length() != 15 && certNo.length() != 18))
  55. return false;
  56. final char[] cs = certNo.toUpperCase().toCharArray();
  57. //校验位数
  58. int power = 0;
  59. for (int i = 0; i < cs.length; i++) {
  60. if (i == cs.length - 1 && cs[i] == 'X')
  61. break;//最后一位可以 是X或x
  62. if (cs[i] < '0' || cs[i] > '9')
  63. return false;
  64. if (i < cs.length - 1) {
  65. power += (cs[i] - '0') * POWER_LIST[i];
  66. }
  67. }
  68. //校验区位码
  69. if (!zoneNum.containsKey(Integer.valueOf(certNo.substring(0, 2)))) {
  70. return false;
  71. }
  72. //校验年份
  73. String year = certNo.length() == 15 ? "19" + certNo.substring(6, 8) : certNo.substring(6, 10);
  74. final int iyear = Integer.parseInt(year);
  75. if (iyear < 1900 || iyear > Calendar.getInstance().get(Calendar.YEAR))
  76. return false;//1900年的PASS,超过今年的PASS
  77. //校验月份
  78. String month = certNo.length() == 15 ? certNo.substring(8, 10) : certNo.substring(10, 12);
  79. final int imonth = Integer.parseInt(month);
  80. if (imonth < 1 || imonth > 12) {
  81. return false;
  82. }
  83. //校验天数
  84. String day = certNo.length() == 15 ? certNo.substring(10, 12) : certNo.substring(12, 14);
  85. final int iday = Integer.parseInt(day);
  86. if (iday < 1 || iday > 31)
  87. return false;
  88. //校验"校验码"
  89. if (certNo.length() == 15)
  90. return true;
  91. return cs[cs.length - 1] == PARITYBIT[power % 11];
  92. }
  93. }