IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Java验证身份证号 -> 正文阅读

[Java知识库]Java验证身份证号

一、需求和算法

  1. 需求

    1. 使用 Java 中 util 包通过键盘输入身份证号。
    2. 使用算法校验身份证号是否合法。
    3. 根据身份证号获取用户出生地。
    4. 根据身份证号获取用户生日。
    5. 根据身份证号获取用户性别。
    6. 打印出生地、生日、性别。
  2. 算法

    1. 取出身份证号前 17 位与指定系数相乘。系数表如下:

    2. 把相乘的结果累加起来。

    3. 用累加结果对 11 求余。

    4. 根据余数对应下表找到身份证号的末尾数。

    5. 判断用户输入的身份证号最后一位与余数对应的尾数是否相等。若相等表示输入的身份证号无误。反之有误。

    6. 参考文献

二、键盘输入:

  1. 代码:

    import java.util.*;
    public class IdNumber {
        public static void main(String[] args) {
    
            // 键盘输入身份证号
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入您的身份证号:");
            String idNumber = scanner.nextLine();
            System.out.println("您的身份证号为:" + idNumber);
            
        }
    }
    
  2. 运行结果:

三、处理末尾数为 x 的身份证号

  1. 代码:

    import java.util.*;
    public class IdNumber {
        public static void main(String[] args) {
    
            // 键盘输入身份证号
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入您的身份证号:");
            String idNumber = scanner.nextLine();
    
            // 处理最后一位为 x 的情况,小写转大写
            idNumber = idNumber.toUpperCase();
            System.out.println("您的身份证号为:" + idNumber);
            
        }
    }
    
  2. 运行结果:

四、校验身份证号

  1. 定义字符串截取函数

    1. 代码:

      import java.util.*;
      public class IdNumber {
      
          // 入口
          public static void main(String[] args) {
      
              // 键盘输入身份证号
              Scanner scanner = new Scanner(System.in);
              System.out.println("请输入您的身份证号:");
              String idNumber = scanner.nextLine();
      
              // 处理最后一位为 x 的情况,小写转大写
              idNumber = idNumber.toUpperCase();
      
              // 测试代码
              System.out.println("您的身份证号前六位:" + getStr(idNumber,0,5));
              
          }
      
          // 截取字符串的方法
          public static String getStr(String str,int a,int b)
          {
              b++;
              return str.substring(a,b);
          }
      
      }
      
    2. 运行结果:

  2. 定义校验函数

    1. 代码:

      import java.util.*;
      public class IdNumber {
      
          // 入口
          public static void main(String[] args) {
      
              // 键盘输入身份证号
              Scanner scanner = new Scanner(System.in);
              System.out.println("请输入您的身份证号:");
              String idNumber = scanner.nextLine();
      
              // 处理最后一位为 x 的情况,小写转大写
              idNumber = idNumber.toUpperCase();
      
              // 判断身份证号是否合法
              Boolean idLegit = judgeId(idNumber);
              System.out.println(idLegit);
              
          }
      
          // 判断身份证号是否合法
          public static Boolean judgeId(String id)
          {
              Boolean result = true;
      
              // 长度不等于 18 位
              if(id.length() != 18) return false;
      
              // 系数算法
              String tempId = getStr(id,0,16);
              int[] coeff = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
              char[] end = {'1','0','X','9','8','7','6','5','4','3','2'};
              int sum = 0;
              for (int i = 0; i < tempId.length(); i++)
              {
                  int bye = tempId.charAt(i) - '0';
                  sum += bye * coeff[i];
              }
              sum %= 11;
              if(end[sum] != getStr(id,17,17).charAt(0)) result = false;
      
              return result;
          }
      
          // 截取字符串的方法
          public static String getStr(String str,int a,int b)
          {
              b++;
              return str.substring(a,b);
          }
      
      }
      
    2. 运行结果:

五、获取地址信息

  1. 定义函数:

    import java.util.*;
    public class IdNumber {
    
        // 入口
        public static void main(String[] args) {
    
            // 键盘输入身份证号
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入您的身份证号:");
            String idNumber = scanner.nextLine();
    
            // 处理最后一位为 x 的情况,小写转大写
            idNumber = idNumber.toUpperCase();
    
            // 判断身份证号是否合法
            Boolean idLegit = judgeId(idNumber);
            if(idLegit)
            {
                String address = getAddress(getStr(idNumber,0,5));
                System.out.println("您的居住地:" + address);
            }
            else System.out.println("身份证号有误!请重启程序");
            
        }
    
        // 判断身份证号是否合法
        public static Boolean judgeId(String id)
        {
            Boolean result = true;
    
            // 长度不等于 18 位
            if(id.length() != 18) return false;
    
            // 系数算法
            String tempId = getStr(id,0,16);
            int[] coeff = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
            char[] end = {'1','0','X','9','8','7','6','5','4','3','2'};
            int sum = 0;
            for (int i = 0; i < tempId.length(); i++)
            {
                int bye = tempId.charAt(i) - '0';
                sum += bye * coeff[i];
            }
            sum %= 11;
            if(end[sum] != getStr(id,17,17).charAt(0)) result = false;
    
            return result;
        }
    
        // 截取字符串的方法
        public static String getStr(String str,int a,int b)
        {
            b++;
            return str.substring(a,b);
        }
    
        // 获取地址信息
        public static String getAddress(String num)
        {
            String str0 = getStr(num,0,1);
            String str1 = getStr(num,2,3);
            String str2 = getStr(num,4,5);
            String address = "";
            switch (str0)
            {
                case "11":
                    address += "北京-";
                    break;
                case "12":
                    address += "天津-";
                    break;
                case "13":
                    address += "河北-";
                    break;
                case "14":
                    address += "山西-";
                    break;
                case "15":
                    address += "内蒙古自治区-";
                    switch (str1)
                    {
                        case "01":
                            address += "呼和浩特市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "新城区";
                                    break;
                                case "03":
                                    address += "回民区";
                                    break;
                                case "04":
                                    address += "玉泉区";
                                    break;
                                case "05":
                                    address += "赛罕区";
                                    break;
                                case "21":
                                    address += "土默特左旗";
                                    break;
                                case "22":
                                    address += "托克托县";
                                    break;
                                case "23":
                                    address += "和林格尔县";
                                    break;
                                case "24":
                                    address += "清水河县";
                                    break;
                                case "25":
                                    address += "武川县";
                                    break;
                            }
                            break;
                        case "02":
                            address += "包头市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "东河区";
                                    break;
                                case "03":
                                    address += "昆都仓区";
                                    break;
                                case "04":
                                    address += "青山区";
                                    break;
                                case "05":
                                    address += "石拐区";
                                    break;
                                case "06":
                                    address += "白云鄂博矿区";
                                    break;
                                case "07":
                                    address += "九原区";
                                    break;
                                case "21":
                                    address += "土默特右旗";
                                    break;
                                case "22":
                                    address += "固阳县";
                                    break;
                                case "23":
                                    address += "达尔罕茂明安联合旗";
                                    break;
                            }
                            break;
                        case "03":
                            address += "乌海市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "海勃湾区";
                                    break;
                                case "03":
                                    address += "海南区";
                                    break;
                                case "04":
                                    address += "乌达区";
                                    break;
                            }
                            break;
                        case "04":
                            address += "赤峰市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "红山区";
                                    break;
                                case "03":
                                    address += "元宝山区";
                                    break;
                                case "04":
                                    address += "松山区";
                                    break;
                                case "21":
                                    address += "阿鲁科尔沁旗";
                                    break;
                                case "22":
                                    address += "巴林左旗";
                                    break;
                                case "23":
                                    address += "巴林右旗";
                                    break;
                                case "24":
                                    address += "林西县";
                                    break;
                                case "25":
                                    address += "克什克腾旗";
                                    break;
                                case "26":
                                    address += "翁牛特旗";
                                    break;
                                case "28":
                                    address += "喀喇沁旗";
                                    break;
                                case "29":
                                    address += "宁城县";
                                    break;
                                case "30":
                                    address += "敖汉旗";
                                    break;
                            }
                            break;
                        case "05":
                            address += "通辽市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "科尔沁区";
                                    break;
                                case "21":
                                    address += "科左中旗";
                                    break;
                                case "22":
                                    address += "科尔沁右翼后旗";
                                    break;
                                case "23":
                                    address += "开鲁县";
                                    break;
                                case "24":
                                    address += "库论县";
                                    break;
                                case "25":
                                    address += "奈曼旗";
                                    break;
                                case "26":
                                    address += "扎鲁特旗";
                                    break;
                                case "81":
                                    address += "霍林郭勒市";
                                    break;
                            }
                            break;
                        case "06":
                            address += "鄂尔多斯市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "东胜区";
                                    break;
                                case "21":
                                    address += "达拉特旗";
                                    break;
                                case "22":
                                    address += "准格尔旗";
                                    break;
                                case "23":
                                    address += "鄂托克前旗";
                                    break;
                                case "24":
                                    address += "鄂托克旗";
                                    break;
                                case "25":
                                    address += "杭锦旗";
                                    break;
                                case "26":
                                    address += "乌审旗";
                                    break;
                                case "27":
                                    address += "伊金霍洛旗";
                                    break;
                            }
                            break;
                        case "07":
                            address += "呼伦贝尔市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "海拉尔区";
                                    break;
                                case "21":
                                    address += "阿荣旗";
                                    break;
                                case "22":
                                    address += "莫旗";
                                    break;
                                case "23":
                                    address += "鄂伦春旗";
                                    break;
                                case "24":
                                    address += "鄂温克族旗";
                                    break;
                                case "25":
                                    address += "陈巴尔虎旗";
                                    break;
                                case "26":
                                    address += "新巴尔虎左旗";
                                    break;
                                case "27":
                                    address += "新巴尔虎右旗";
                                    break;
                                case "81":
                                    address += "满洲里市";
                                    break;
                                case "82":
                                    address += "牙克石市";
                                    break;
                                case "83":
                                    address += "扎兰屯市";
                                    break;
                                case "84":
                                    address += "额尔古纳市";
                                    break;
                                case "85":
                                    address += "根河市";
                                    break;
                            }
                            break;
                        case "08":
                            address += "巴彦淖尔市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "临河区";
                                    break;
                                case "21":
                                    address += "五原县";
                                    break;
                                case "22":
                                    address += "磴口县";
                                    break;
                                case "23":
                                    address += "乌拉特前旗";
                                    break;
                                case "24":
                                    address += "乌拉特中旗";
                                    break;
                                case "25":
                                    address += "乌拉特后旗";
                                    break;
                                case "26":
                                    address += "杭锦后旗";
                                    break;
                            }
                            break;
                        case "09":
                            address += "乌兰察布市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "集宁区";
                                    break;
                                case "21":
                                    address += "卓资县";
                                    break;
                                case "22":
                                    address += "化德县";
                                    break;
                                case "23":
                                    address += "商都县";
                                    break;
                                case "24":
                                    address += "兴和县";
                                    break;
                                case "25":
                                    address += "凉城县";
                                    break;
                                case "26":
                                    address += "察哈尔右翼前旗";
                                    break;
                                case "27":
                                    address += "察哈尔右翼中旗";
                                    break;
                                case "28":
                                    address += "察哈尔右翼后旗";
                                    break;
                                case "29":
                                    address += "四子王旗";
                                    break;
                                case "81":
                                    address += "丰镇市";
                                    break;
                            }
                            break;
                        case "22":
                            address += "兴安盟-";
                            switch (str2)
                            {
                                case "01":
                                    address += "乌兰浩特市";
                                    break;
                                case "02":
                                    address += "阿尔山市";
                                    break;
                                case "21":
                                    address += "科尔沁右翼前旗";
                                    break;
                                case "22":
                                    address += "科尔沁右翼中旗";
                                    break;
                                case "23":
                                    address += "扎赉特旗";
                                    break;
                                case "24":
                                    address += "突泉县";
                                    break;
                            }
                            break;
                        case "25":
                            address += "锡林郭勒盟-";
                            switch (str2)
                            {
                                case "01":
                                    address += "二连浩特市";
                                    break;
                                case "02":
                                    address += "锡林浩特市";
                                    break;
                                case "22":
                                    address += "阿巴嘎旗";
                                    break;
                                case "23":
                                    address += "苏尼特左旗";
                                    break;
                                case "24":
                                    address += "苏尼特右旗";
                                    break;
                                case "25":
                                    address += "东乌珠穆沁旗";
                                    break;
                                case "26":
                                    address += "西乌珠穆沁旗";
                                    break;
                                case "27":
                                    address += "太仆寺旗";
                                    break;
                                case "28":
                                    address += "镶黄旗";
                                    break;
                                case "29":
                                    address += "正镶白旗";
                                    break;
                                case "30":
                                    address += "正蓝旗";
                                    break;
                                case "31":
                                    address += "多伦县";
                                    break;
                            }
                            break;
                        case "29":
                            address += "阿拉善盟-";
                            switch (str2)
                            {
                                case "21":
                                    address += "阿拉善左旗";
                                    break;
                                case "22":
                                    address += "阿拉善右旗";
                                    break;
                                case "23":
                                    address += "额济纳旗";
                                    break;
                            }
                            break;
                    }
                    break;
                case "21":
                    address += "辽宁-";
                    break;
                case "22":
                    address += "吉林-";
                    break;
                case "23":
                    address += "黑龙江-";
                    break;
                case "31":
                    address += "上海-";
                    break;
                case "32":
                    address += "江苏-";
                    break;
                case "33":
                    address += "浙江-";
                    break;
                case "34":
                    address += "安徽-";
                    break;
                case "35":
                    address += "福建-";
                    break;
                case "36":
                    address += "江西-";
                    break;
                case "37":
                    address += "山东-";
                    break;
                case "41":
                    address += "河南-";
                    break;
                case "42":
                    address += "湖北-";
                    break;
                case "43":
                    address += "湖南-";
                    break;
                case "44":
                    address += "广西-";
                    break;
                case "45":
                    address += "广东-";
                    break;
                case "46":
                    address += "海南-";
                    break;
                case "50":
                    address += "重庆-";
                    break;
                case "51":
                    address += "四川-";
                    break;
                case "52":
                    address += "贵州-";
                    break;
                case "53":
                    address += "云南-";
                    break;
                case "54":
                    address += "西藏-";
                    break;
                case "61":
                    address += "陕西-";
                    break;
                case "62":
                    address += "甘肃-";
                    break;
                case "63":
                    address += "青海-";
                    break;
                case "64":
                    address += "宁夏-";
                    break;
                case "65":
                    address += "新疆-";
                    break;
                case "71":
                    address += "台湾-";
                    break;
                case "81":
                    address += "香港-";
                    break;
                case "82":
                    address += "澳门-";
                    break;
            }
            return address;
        }
    
    }
    
  2. 运行结果:

六、获取生日信息

  1. 代码:

    import java.util.*;
    public class IdNumber {
    
        // 入口
        public static void main(String[] args) {
    
            // 键盘输入身份证号
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入您的身份证号:");
            String idNumber = scanner.nextLine();
    
            // 处理最后一位为 x 的情况,小写转大写
            idNumber = idNumber.toUpperCase();
    
            // 判断身份证号是否合法
            Boolean idLegit = judgeId(idNumber);
            if(idLegit)
            {
                String address = getAddress(getStr(idNumber,0,5));
                String birthday = getBirthday(getStr(idNumber,6,13));
                System.out.println("您的居住地:" + address);
                System.out.println("您的生日:" + birthday);
            }
            else System.out.println("身份证号有误!请重启程序");
            
        }
    
        // 判断身份证号是否合法
        public static Boolean judgeId(String id)
        {
            Boolean result = true;
    
            // 长度不等于 18 位
            if(id.length() != 18) return false;
    
            // 系数算法
            String tempId = getStr(id,0,16);
            int[] coeff = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
            char[] end = {'1','0','X','9','8','7','6','5','4','3','2'};
            int sum = 0;
            for (int i = 0; i < tempId.length(); i++)
            {
                int bye = tempId.charAt(i) - '0';
                sum += bye * coeff[i];
            }
            sum %= 11;
            if(end[sum] != getStr(id,17,17).charAt(0)) result = false;
    
            return result;
        }
    
        // 截取字符串的方法
        public static String getStr(String str,int a,int b)
        {
            b++;
            return str.substring(a,b);
        }
    
        // 获取地址信息
        public static String getAddress(String num)
        {
            String str0 = getStr(num,0,1);
            String str1 = getStr(num,2,3);
            String str2 = getStr(num,4,5);
            String address = "";
            switch (str0)
            {
                case "11":
                    address += "北京-";
                    break;
                case "12":
                    address += "天津-";
                    break;
                case "13":
                    address += "河北-";
                    break;
                case "14":
                    address += "山西-";
                    break;
                case "15":
                    address += "内蒙古自治区-";
                    switch (str1)
                    {
                        case "01":
                            address += "呼和浩特市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "新城区";
                                    break;
                                case "03":
                                    address += "回民区";
                                    break;
                                case "04":
                                    address += "玉泉区";
                                    break;
                                case "05":
                                    address += "赛罕区";
                                    break;
                                case "21":
                                    address += "土默特左旗";
                                    break;
                                case "22":
                                    address += "托克托县";
                                    break;
                                case "23":
                                    address += "和林格尔县";
                                    break;
                                case "24":
                                    address += "清水河县";
                                    break;
                                case "25":
                                    address += "武川县";
                                    break;
                            }
                            break;
                        case "02":
                            address += "包头市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "东河区";
                                    break;
                                case "03":
                                    address += "昆都仓区";
                                    break;
                                case "04":
                                    address += "青山区";
                                    break;
                                case "05":
                                    address += "石拐区";
                                    break;
                                case "06":
                                    address += "白云鄂博矿区";
                                    break;
                                case "07":
                                    address += "九原区";
                                    break;
                                case "21":
                                    address += "土默特右旗";
                                    break;
                                case "22":
                                    address += "固阳县";
                                    break;
                                case "23":
                                    address += "达尔罕茂明安联合旗";
                                    break;
                            }
                            break;
                        case "03":
                            address += "乌海市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "海勃湾区";
                                    break;
                                case "03":
                                    address += "海南区";
                                    break;
                                case "04":
                                    address += "乌达区";
                                    break;
                            }
                            break;
                        case "04":
                            address += "赤峰市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "红山区";
                                    break;
                                case "03":
                                    address += "元宝山区";
                                    break;
                                case "04":
                                    address += "松山区";
                                    break;
                                case "21":
                                    address += "阿鲁科尔沁旗";
                                    break;
                                case "22":
                                    address += "巴林左旗";
                                    break;
                                case "23":
                                    address += "巴林右旗";
                                    break;
                                case "24":
                                    address += "林西县";
                                    break;
                                case "25":
                                    address += "克什克腾旗";
                                    break;
                                case "26":
                                    address += "翁牛特旗";
                                    break;
                                case "28":
                                    address += "喀喇沁旗";
                                    break;
                                case "29":
                                    address += "宁城县";
                                    break;
                                case "30":
                                    address += "敖汉旗";
                                    break;
                            }
                            break;
                        case "05":
                            address += "通辽市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "科尔沁区";
                                    break;
                                case "21":
                                    address += "科左中旗";
                                    break;
                                case "22":
                                    address += "科尔沁右翼后旗";
                                    break;
                                case "23":
                                    address += "开鲁县";
                                    break;
                                case "24":
                                    address += "库论县";
                                    break;
                                case "25":
                                    address += "奈曼旗";
                                    break;
                                case "26":
                                    address += "扎鲁特旗";
                                    break;
                                case "81":
                                    address += "霍林郭勒市";
                                    break;
                            }
                            break;
                        case "06":
                            address += "鄂尔多斯市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "东胜区";
                                    break;
                                case "21":
                                    address += "达拉特旗";
                                    break;
                                case "22":
                                    address += "准格尔旗";
                                    break;
                                case "23":
                                    address += "鄂托克前旗";
                                    break;
                                case "24":
                                    address += "鄂托克旗";
                                    break;
                                case "25":
                                    address += "杭锦旗";
                                    break;
                                case "26":
                                    address += "乌审旗";
                                    break;
                                case "27":
                                    address += "伊金霍洛旗";
                                    break;
                            }
                            break;
                        case "07":
                            address += "呼伦贝尔市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "海拉尔区";
                                    break;
                                case "21":
                                    address += "阿荣旗";
                                    break;
                                case "22":
                                    address += "莫旗";
                                    break;
                                case "23":
                                    address += "鄂伦春旗";
                                    break;
                                case "24":
                                    address += "鄂温克族旗";
                                    break;
                                case "25":
                                    address += "陈巴尔虎旗";
                                    break;
                                case "26":
                                    address += "新巴尔虎左旗";
                                    break;
                                case "27":
                                    address += "新巴尔虎右旗";
                                    break;
                                case "81":
                                    address += "满洲里市";
                                    break;
                                case "82":
                                    address += "牙克石市";
                                    break;
                                case "83":
                                    address += "扎兰屯市";
                                    break;
                                case "84":
                                    address += "额尔古纳市";
                                    break;
                                case "85":
                                    address += "根河市";
                                    break;
                            }
                            break;
                        case "08":
                            address += "巴彦淖尔市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "临河区";
                                    break;
                                case "21":
                                    address += "五原县";
                                    break;
                                case "22":
                                    address += "磴口县";
                                    break;
                                case "23":
                                    address += "乌拉特前旗";
                                    break;
                                case "24":
                                    address += "乌拉特中旗";
                                    break;
                                case "25":
                                    address += "乌拉特后旗";
                                    break;
                                case "26":
                                    address += "杭锦后旗";
                                    break;
                            }
                            break;
                        case "09":
                            address += "乌兰察布市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "集宁区";
                                    break;
                                case "21":
                                    address += "卓资县";
                                    break;
                                case "22":
                                    address += "化德县";
                                    break;
                                case "23":
                                    address += "商都县";
                                    break;
                                case "24":
                                    address += "兴和县";
                                    break;
                                case "25":
                                    address += "凉城县";
                                    break;
                                case "26":
                                    address += "察哈尔右翼前旗";
                                    break;
                                case "27":
                                    address += "察哈尔右翼中旗";
                                    break;
                                case "28":
                                    address += "察哈尔右翼后旗";
                                    break;
                                case "29":
                                    address += "四子王旗";
                                    break;
                                case "81":
                                    address += "丰镇市";
                                    break;
                            }
                            break;
                        case "22":
                            address += "兴安盟-";
                            switch (str2)
                            {
                                case "01":
                                    address += "乌兰浩特市";
                                    break;
                                case "02":
                                    address += "阿尔山市";
                                    break;
                                case "21":
                                    address += "科尔沁右翼前旗";
                                    break;
                                case "22":
                                    address += "科尔沁右翼中旗";
                                    break;
                                case "23":
                                    address += "扎赉特旗";
                                    break;
                                case "24":
                                    address += "突泉县";
                                    break;
                            }
                            break;
                        case "25":
                            address += "锡林郭勒盟-";
                            switch (str2)
                            {
                                case "01":
                                    address += "二连浩特市";
                                    break;
                                case "02":
                                    address += "锡林浩特市";
                                    break;
                                case "22":
                                    address += "阿巴嘎旗";
                                    break;
                                case "23":
                                    address += "苏尼特左旗";
                                    break;
                                case "24":
                                    address += "苏尼特右旗";
                                    break;
                                case "25":
                                    address += "东乌珠穆沁旗";
                                    break;
                                case "26":
                                    address += "西乌珠穆沁旗";
                                    break;
                                case "27":
                                    address += "太仆寺旗";
                                    break;
                                case "28":
                                    address += "镶黄旗";
                                    break;
                                case "29":
                                    address += "正镶白旗";
                                    break;
                                case "30":
                                    address += "正蓝旗";
                                    break;
                                case "31":
                                    address += "多伦县";
                                    break;
                            }
                            break;
                        case "29":
                            address += "阿拉善盟-";
                            switch (str2)
                            {
                                case "21":
                                    address += "阿拉善左旗";
                                    break;
                                case "22":
                                    address += "阿拉善右旗";
                                    break;
                                case "23":
                                    address += "额济纳旗";
                                    break;
                            }
                            break;
                    }
                    break;
                case "21":
                    address += "辽宁-";
                    break;
                case "22":
                    address += "吉林-";
                    break;
                case "23":
                    address += "黑龙江-";
                    break;
                case "31":
                    address += "上海-";
                    break;
                case "32":
                    address += "江苏-";
                    break;
                case "33":
                    address += "浙江-";
                    break;
                case "34":
                    address += "安徽-";
                    break;
                case "35":
                    address += "福建-";
                    break;
                case "36":
                    address += "江西-";
                    break;
                case "37":
                    address += "山东-";
                    break;
                case "41":
                    address += "河南-";
                    break;
                case "42":
                    address += "湖北-";
                    break;
                case "43":
                    address += "湖南-";
                    break;
                case "44":
                    address += "广西-";
                    break;
                case "45":
                    address += "广东-";
                    break;
                case "46":
                    address += "海南-";
                    break;
                case "50":
                    address += "重庆-";
                    break;
                case "51":
                    address += "四川-";
                    break;
                case "52":
                    address += "贵州-";
                    break;
                case "53":
                    address += "云南-";
                    break;
                case "54":
                    address += "西藏-";
                    break;
                case "61":
                    address += "陕西-";
                    break;
                case "62":
                    address += "甘肃-";
                    break;
                case "63":
                    address += "青海-";
                    break;
                case "64":
                    address += "宁夏-";
                    break;
                case "65":
                    address += "新疆-";
                    break;
                case "71":
                    address += "台湾-";
                    break;
                case "81":
                    address += "香港-";
                    break;
                case "82":
                    address += "澳门-";
                    break;
            }
            return address;
        }
    
        // 获取生日信息
        public static String getBirthday(String num)
        {
            String y = getStr(num,0,3);
            String m = getStr(num,4,5);
            String d = getStr(num,6,7);
            return y + "-" + m + "-" + d;
        }
    
    }
    
  2. 运行结果:

七、获取性别信息

  1. 代码:

    import java.util.*;
    public class IdNumber {
    
        // 入口
        public static void main(String[] args) {
    
            // 键盘输入身份证号
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入您的身份证号:");
            String idNumber = scanner.nextLine();
    
            // 处理最后一位为 x 的情况,小写转大写
            idNumber = idNumber.toUpperCase();
    
            // 判断身份证号是否合法
            Boolean idLegit = judgeId(idNumber);
            if(idLegit)
            {
                String address = getAddress(getStr(idNumber,0,5));
                String birthday = getBirthday(getStr(idNumber,6,13));
                String gender = getGender(getStr(idNumber,16,16));
                System.out.println("您的居住地:" + address);
                System.out.println("您的生日:" + birthday);
                System.out.println("您的性别:" + gender);
            }
            else System.out.println("身份证号有误!请重启程序");
            
        }
    
        // 判断身份证号是否合法
        public static Boolean judgeId(String id)
        {
            Boolean result = true;
    
            // 长度不等于 18 位
            if(id.length() != 18) return false;
    
            // 系数算法
            String tempId = getStr(id,0,16);
            int[] coeff = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
            char[] end = {'1','0','X','9','8','7','6','5','4','3','2'};
            int sum = 0;
            for (int i = 0; i < tempId.length(); i++)
            {
                int bye = tempId.charAt(i) - '0';
                sum += bye * coeff[i];
            }
            sum %= 11;
            if(end[sum] != getStr(id,17,17).charAt(0)) result = false;
    
            return result;
        }
    
        // 截取字符串的方法
        public static String getStr(String str,int a,int b)
        {
            b++;
            return str.substring(a,b);
        }
    
        // 获取地址信息
        public static String getAddress(String num)
        {
            String str0 = getStr(num,0,1);
            String str1 = getStr(num,2,3);
            String str2 = getStr(num,4,5);
            String address = "";
            switch (str0)
            {
                case "11":
                    address += "北京-";
                    break;
                case "12":
                    address += "天津-";
                    break;
                case "13":
                    address += "河北-";
                    break;
                case "14":
                    address += "山西-";
                    break;
                case "15":
                    address += "内蒙古自治区-";
                    switch (str1)
                    {
                        case "01":
                            address += "呼和浩特市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "新城区";
                                    break;
                                case "03":
                                    address += "回民区";
                                    break;
                                case "04":
                                    address += "玉泉区";
                                    break;
                                case "05":
                                    address += "赛罕区";
                                    break;
                                case "21":
                                    address += "土默特左旗";
                                    break;
                                case "22":
                                    address += "托克托县";
                                    break;
                                case "23":
                                    address += "和林格尔县";
                                    break;
                                case "24":
                                    address += "清水河县";
                                    break;
                                case "25":
                                    address += "武川县";
                                    break;
                            }
                            break;
                        case "02":
                            address += "包头市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "东河区";
                                    break;
                                case "03":
                                    address += "昆都仓区";
                                    break;
                                case "04":
                                    address += "青山区";
                                    break;
                                case "05":
                                    address += "石拐区";
                                    break;
                                case "06":
                                    address += "白云鄂博矿区";
                                    break;
                                case "07":
                                    address += "九原区";
                                    break;
                                case "21":
                                    address += "土默特右旗";
                                    break;
                                case "22":
                                    address += "固阳县";
                                    break;
                                case "23":
                                    address += "达尔罕茂明安联合旗";
                                    break;
                            }
                            break;
                        case "03":
                            address += "乌海市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "海勃湾区";
                                    break;
                                case "03":
                                    address += "海南区";
                                    break;
                                case "04":
                                    address += "乌达区";
                                    break;
                            }
                            break;
                        case "04":
                            address += "赤峰市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "红山区";
                                    break;
                                case "03":
                                    address += "元宝山区";
                                    break;
                                case "04":
                                    address += "松山区";
                                    break;
                                case "21":
                                    address += "阿鲁科尔沁旗";
                                    break;
                                case "22":
                                    address += "巴林左旗";
                                    break;
                                case "23":
                                    address += "巴林右旗";
                                    break;
                                case "24":
                                    address += "林西县";
                                    break;
                                case "25":
                                    address += "克什克腾旗";
                                    break;
                                case "26":
                                    address += "翁牛特旗";
                                    break;
                                case "28":
                                    address += "喀喇沁旗";
                                    break;
                                case "29":
                                    address += "宁城县";
                                    break;
                                case "30":
                                    address += "敖汉旗";
                                    break;
                            }
                            break;
                        case "05":
                            address += "通辽市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "科尔沁区";
                                    break;
                                case "21":
                                    address += "科左中旗";
                                    break;
                                case "22":
                                    address += "科尔沁右翼后旗";
                                    break;
                                case "23":
                                    address += "开鲁县";
                                    break;
                                case "24":
                                    address += "库论县";
                                    break;
                                case "25":
                                    address += "奈曼旗";
                                    break;
                                case "26":
                                    address += "扎鲁特旗";
                                    break;
                                case "81":
                                    address += "霍林郭勒市";
                                    break;
                            }
                            break;
                        case "06":
                            address += "鄂尔多斯市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "东胜区";
                                    break;
                                case "21":
                                    address += "达拉特旗";
                                    break;
                                case "22":
                                    address += "准格尔旗";
                                    break;
                                case "23":
                                    address += "鄂托克前旗";
                                    break;
                                case "24":
                                    address += "鄂托克旗";
                                    break;
                                case "25":
                                    address += "杭锦旗";
                                    break;
                                case "26":
                                    address += "乌审旗";
                                    break;
                                case "27":
                                    address += "伊金霍洛旗";
                                    break;
                            }
                            break;
                        case "07":
                            address += "呼伦贝尔市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "海拉尔区";
                                    break;
                                case "21":
                                    address += "阿荣旗";
                                    break;
                                case "22":
                                    address += "莫旗";
                                    break;
                                case "23":
                                    address += "鄂伦春旗";
                                    break;
                                case "24":
                                    address += "鄂温克族旗";
                                    break;
                                case "25":
                                    address += "陈巴尔虎旗";
                                    break;
                                case "26":
                                    address += "新巴尔虎左旗";
                                    break;
                                case "27":
                                    address += "新巴尔虎右旗";
                                    break;
                                case "81":
                                    address += "满洲里市";
                                    break;
                                case "82":
                                    address += "牙克石市";
                                    break;
                                case "83":
                                    address += "扎兰屯市";
                                    break;
                                case "84":
                                    address += "额尔古纳市";
                                    break;
                                case "85":
                                    address += "根河市";
                                    break;
                            }
                            break;
                        case "08":
                            address += "巴彦淖尔市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "临河区";
                                    break;
                                case "21":
                                    address += "五原县";
                                    break;
                                case "22":
                                    address += "磴口县";
                                    break;
                                case "23":
                                    address += "乌拉特前旗";
                                    break;
                                case "24":
                                    address += "乌拉特中旗";
                                    break;
                                case "25":
                                    address += "乌拉特后旗";
                                    break;
                                case "26":
                                    address += "杭锦后旗";
                                    break;
                            }
                            break;
                        case "09":
                            address += "乌兰察布市-";
                            switch (str2)
                            {
                                case "02":
                                    address += "集宁区";
                                    break;
                                case "21":
                                    address += "卓资县";
                                    break;
                                case "22":
                                    address += "化德县";
                                    break;
                                case "23":
                                    address += "商都县";
                                    break;
                                case "24":
                                    address += "兴和县";
                                    break;
                                case "25":
                                    address += "凉城县";
                                    break;
                                case "26":
                                    address += "察哈尔右翼前旗";
                                    break;
                                case "27":
                                    address += "察哈尔右翼中旗";
                                    break;
                                case "28":
                                    address += "察哈尔右翼后旗";
                                    break;
                                case "29":
                                    address += "四子王旗";
                                    break;
                                case "81":
                                    address += "丰镇市";
                                    break;
                            }
                            break;
                        case "22":
                            address += "兴安盟-";
                            switch (str2)
                            {
                                case "01":
                                    address += "乌兰浩特市";
                                    break;
                                case "02":
                                    address += "阿尔山市";
                                    break;
                                case "21":
                                    address += "科尔沁右翼前旗";
                                    break;
                                case "22":
                                    address += "科尔沁右翼中旗";
                                    break;
                                case "23":
                                    address += "扎赉特旗";
                                    break;
                                case "24":
                                    address += "突泉县";
                                    break;
                            }
                            break;
                        case "25":
                            address += "锡林郭勒盟-";
                            switch (str2)
                            {
                                case "01":
                                    address += "二连浩特市";
                                    break;
                                case "02":
                                    address += "锡林浩特市";
                                    break;
                                case "22":
                                    address += "阿巴嘎旗";
                                    break;
                                case "23":
                                    address += "苏尼特左旗";
                                    break;
                                case "24":
                                    address += "苏尼特右旗";
                                    break;
                                case "25":
                                    address += "东乌珠穆沁旗";
                                    break;
                                case "26":
                                    address += "西乌珠穆沁旗";
                                    break;
                                case "27":
                                    address += "太仆寺旗";
                                    break;
                                case "28":
                                    address += "镶黄旗";
                                    break;
                                case "29":
                                    address += "正镶白旗";
                                    break;
                                case "30":
                                    address += "正蓝旗";
                                    break;
                                case "31":
                                    address += "多伦县";
                                    break;
                            }
                            break;
                        case "29":
                            address += "阿拉善盟-";
                            switch (str2)
                            {
                                case "21":
                                    address += "阿拉善左旗";
                                    break;
                                case "22":
                                    address += "阿拉善右旗";
                                    break;
                                case "23":
                                    address += "额济纳旗";
                                    break;
                            }
                            break;
                    }
                    break;
                case "21":
                    address += "辽宁-";
                    break;
                case "22":
                    address += "吉林-";
                    break;
                case "23":
                    address += "黑龙江-";
                    break;
                case "31":
                    address += "上海-";
                    break;
                case "32":
                    address += "江苏-";
                    break;
                case "33":
                    address += "浙江-";
                    break;
                case "34":
                    address += "安徽-";
                    break;
                case "35":
                    address += "福建-";
                    break;
                case "36":
                    address += "江西-";
                    break;
                case "37":
                    address += "山东-";
                    break;
                case "41":
                    address += "河南-";
                    break;
                case "42":
                    address += "湖北-";
                    break;
                case "43":
                    address += "湖南-";
                    break;
                case "44":
                    address += "广西-";
                    break;
                case "45":
                    address += "广东-";
                    break;
                case "46":
                    address += "海南-";
                    break;
                case "50":
                    address += "重庆-";
                    break;
                case "51":
                    address += "四川-";
                    break;
                case "52":
                    address += "贵州-";
                    break;
                case "53":
                    address += "云南-";
                    break;
                case "54":
                    address += "西藏-";
                    break;
                case "61":
                    address += "陕西-";
                    break;
                case "62":
                    address += "甘肃-";
                    break;
                case "63":
                    address += "青海-";
                    break;
                case "64":
                    address += "宁夏-";
                    break;
                case "65":
                    address += "新疆-";
                    break;
                case "71":
                    address += "台湾-";
                    break;
                case "81":
                    address += "香港-";
                    break;
                case "82":
                    address += "澳门-";
                    break;
            }
            return address;
        }
    
        // 获取生日信息
        public static String getBirthday(String num)
        {
            String y = getStr(num,0,3);
            String m = getStr(num,4,5);
            String d = getStr(num,6,7);
            return y + "-" + m + "-" + d;
        }
    
        // 获取性别信息
        public static String getGender(String num)
        {
            int gendetNum = num.charAt(0) - '0';
            return gendetNum % 2 == 0 ? "女" : "男";
        }
        
    }
    
  2. 运行结果:

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-10-22 10:49:50  更:2021-10-22 10:50:07 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 22:28:57-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码