• java 身份证号码验证


    需要编号文件

    编号文件部分内容如下

    11:北京市
    1101:市辖区
    110101:东城区
    110102:西城区
    110105:朝阳区
    110106:丰台区
    110107:石景山区
    110108:海淀区

    ......

    编号文件内容比较多 csdn点击 下载地址

    java代码如下 

    1. import java.io.*;
    2. import java.text.ParseException;
    3. import java.text.SimpleDateFormat;
    4. import java.util.*;
    5. import java.util.function.Consumer;
    6. import java.util.regex.Pattern;
    7. public class IdCardCheckUtils {
    8. public static final Integer[] idCardWeight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};//身份证前17位数字依次乘以对应的权重因子
    9. public static final String[] CONSTELLATION_ARR = {"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座"};//星座数组
    10. public static final int[] CONSTELLATION_EDGE_DAY = {20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};//星座对应的边缘日期
    11. public static final String[] ZODIAC_ARR = {"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};//生肖
    12. public static Map idCardMap = new HashMap<>();//组装根据余数,对应一个指定的校验码
    13. private static Map nativePlaceCode = new HashMap<>(4096, 1);//内存籍贯编号,记录身份证编号对应的地址
    14. public static void main(String[] args) throws Exception {
    15. String path = "C:\\Users\\Administrator\\Desktop\\code.txt";//编号文件
    16. init(path);//初始化身份证校验参数
    17. String idCard = "512926164805034455";//测试的身份证号码
    18. checkIdCard(idCard);//校验身份证是否输入正常
    19. //基本信息
    20. System.out.println("出生日期:" + idCard.substring(6, 10) + "." + idCard.substring(10, 12) + "." + idCard.substring(12, 14));
    21. System.out.println("性别:" +getSex(idCard));
    22. System.out.println("年龄:" + getAge(idCard));
    23. System.out.println("您的星座:" + getConstellation(idCard));
    24. System.out.println("您的生肖:" + getAnimalSign(idCard));
    25. //籍贯信息
    26. int nativePlaceCode = Integer.parseInt(idCard.substring(0, 6));//籍贯组合编码
    27. int provinceCode = nativePlaceCode / 10000;//省编码
    28. int cityCode = nativePlaceCode / 100;//市编码
    29. int countyCode = nativePlaceCode;//县编码
    30. System.out.println(IdCardCheckUtils.nativePlaceCode.get(provinceCode));
    31. System.out.println(IdCardCheckUtils.nativePlaceCode.get(cityCode));
    32. System.out.println(IdCardCheckUtils.nativePlaceCode.get(countyCode));
    33. }
    34. /**
    35. * 初始化地区编码文件与校验码
    36. *
    37. * @param path
    38. * @throws IOException
    39. */
    40. private static void init(String path) throws IOException {
    41. synchronized (String.class) {
    42. if (!idCardMap.isEmpty()) return;
    43. Consumer function = line -> {
    44. String[] split = line.split(":");
    45. nativePlaceCode.put(Integer.valueOf(split[0]), split[1]);
    46. };
    47. read(path, function);//将文件内容加载到map内存中
    48. final String[] idCardCheck = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};//身份证最后一位对应的校验码
    49. for (int i = 0; i < 10; i++) {
    50. idCardMap.put(i, idCardCheck[i]);//校验码记录
    51. }
    52. }
    53. }
    54. /**
    55. * 验证身份证号码是否正常
    56. *
    57. * @param idCardNo
    58. * @return
    59. */
    60. public static void checkIdCard(String idCardNo) throws Exception {
    61. String idCard = idCardNo.toUpperCase();//将其转成大写有的身份证最后一位是字母
    62. if (idCardNo.length() == 15) {//15位身份证转成18位
    63. if (!(idCardNo.matches("[0-9]{17}[0-9|x]|[0-9]{15}"))) throw new Exception("身份证号码输入错误,请输入正确格式的15位身份证号码");
    64. String s2 = idCardNo.substring(0, 6);//15位转换为18位
    65. String s3 = idCardNo.substring(6, 15);
    66. String changed = s2.concat("19").concat(s3);
    67. idCard = changed.toUpperCase();
    68. }
    69. if (!Pattern.matches("^\\d{17}", idCard.substring(0, 17))) throw new Exception("身份证号码输入错误,前17位必须是数字");//验证身份证前17位是否为数字
    70. char[] idCardCharNumber = idCard.toCharArray();
    71. Integer resultSum = 0;
    72. for (int i = 0; i < idCardCharNumber.length - 1; i++) resultSum += Character.getNumericValue(idCardCharNumber[i]) * idCardWeight[i];
    73. Integer lastResult = resultSum % 11;//将相加的前17位数字依次乘以对应的权重因子相加,相加的结果除以11,得到余数
    74. //根据余数,对应一个指定的校验码。最终得到的校验码就是身份证号码的最后一位数字。通过这个校验码,可以验证前面17位数字是否正确,从而提高身份证号码的准确性
    75. if (!(idCardMap.containsKey(lastResult)) || !(idCardMap.get(lastResult).equals(idCard.substring(idCard.length() - 1)))) throw new Exception("身份证号码校验异常,输入错误");
    76. }
    77. /**
    78. * 根据日期获取当前年龄
    79. *
    80. * @return
    81. */
    82. public static int getAge(String idCard) {
    83. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    84. String dateString = dateFormat.format(new Date());
    85. int currentDate = Integer.parseInt(dateString);
    86. int idCardDate = Integer.parseInt(idCard.substring(6, 10) + idCard.substring(10, 12) + idCard.substring(12, 14));
    87. int age = (currentDate - idCardDate) / 10000;
    88. return age;
    89. }
    90. /**
    91. * 根据身份证id获取当前年龄
    92. *
    93. * @return
    94. */
    95. public static String getSex(String idCard) {
    96. String sex = Integer.parseInt(idCard.substring(16, 17)) % 2 == 0 ? "女" : "男";
    97. return sex;
    98. }
    99. /**
    100. * 根据身份证号判断用户星座
    101. *
    102. * @param cardNo
    103. * @return
    104. */
    105. public static String getConstellation(String cardNo) {
    106. String birthday = cardNo.substring(6, 14);// 获取出生日期
    107. Date birthdate = null;
    108. try {
    109. birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);
    110. if (birthdate == null) return "";
    111. Calendar cal = Calendar.getInstance();
    112. cal.setTime(birthdate);
    113. int month = cal.get(Calendar.MONTH);
    114. int day = cal.get(Calendar.DAY_OF_MONTH);
    115. if (day < CONSTELLATION_EDGE_DAY[month]) month = month - 1;
    116. if (month >= 0) return CONSTELLATION_ARR[month];
    117. return CONSTELLATION_ARR[11];// default to return 魔羯
    118. } catch (ParseException e) {
    119. e.printStackTrace();
    120. }
    121. return null;
    122. }
    123. /**
    124. * 根据身份证号判断用户生肖
    125. *
    126. * @param cardNo
    127. * @return
    128. */
    129. public static String getAnimalSign(String cardNo) {
    130. String birthday = cardNo.substring(6, 14);// 获取出生日期
    131. Date birthdate;
    132. try {
    133. birthdate = new SimpleDateFormat("yyyyMMdd").parse(birthday);
    134. Calendar cal = Calendar.getInstance();
    135. cal.setTime(birthdate);
    136. return ZODIAC_ARR[cal.get(Calendar.YEAR) % 12];
    137. } catch (ParseException e) {
    138. e.printStackTrace();
    139. }
    140. return null;
    141. }
    142. public static void read(String path, Consumer func) throws IOException {
    143. File file = new File(path);
    144. FileInputStream fileInputStream = null;
    145. InputStreamReader read = null;//考虑到编码格式
    146. BufferedReader bufferedReader = null;
    147. try {
    148. fileInputStream = new FileInputStream(file);
    149. read = new InputStreamReader(fileInputStream, "UTF-8");
    150. bufferedReader = new BufferedReader(read);
    151. String lineTxt;
    152. while ((lineTxt = bufferedReader.readLine()) != null) func.accept(lineTxt);//读取一行
    153. } catch (IOException e) {
    154. e.printStackTrace();
    155. } finally {
    156. bufferedReader.close();
    157. read.close();
    158. fileInputStream.close();
    159. }
    160. }
    161. }

    运行结果 ,身份证是随意编写的,可以用自己的身份证进行测试

  • 相关阅读:
    CloudCompare——泊松重建
    MATLAB函数mesh与surf等绘制三维曲面入门
    2023-10-03 LeetCode每日一题(买卖股票的最佳时机 III)
    玩转doxygen 之RT-THREAD
    3.5、Linux:命令行git的使用
    Redis 7 第九讲 微服务集成Redis 应用篇
    未来社区的人车房隐私数据权属确认方法
    笔记本小键盘怎么开?探索笔记本小键盘的奥秘
    js的各种循环遍历
    爬虫解析——Xpath
  • 原文地址:https://blog.csdn.net/weixin_42660202/article/details/132725326