根据身份证号计算年龄,15位身份证号码转18位

版权声明:本文为博主原创文章,转载请注明作者和出处,如有错误,望不吝赐教。 https://blog.csdn.net/weixin_41888813/article/details/82457069

第一代身份证:15位身份证号码的意义

15位身份证号码各位的含义: 
1-2位省、自治区、直辖市代码; 
3-4位地级市、盟、自治州代码; 
5-6位县、县级市、区代码; 
7-12位出生年月日,比如670401代表1967年4月1日,这是和18位号码的第一个区别; 
13-15位为顺序号,其中15位男为单数,女为双数; 
与18位身份证号的第二个区别没有最后一位的校验码。 

举例: 
130503 670401 001的含义; 13为河北,05为邢台,03为桥西区,出生日期为1967年4月1日,顺序号为001


第二代身份证:18位身份证号码的意义
  ①前1、2位数字表示:所在省份的代码,河南的省份代码是41哦!
  ②第3、4位数字表示:所在城市的代码;
  ③第5、6位数字表示:所在区县的代码;
  ④第7~14位数字表示:出生年、月、日;
  ⑤第15、16位数字表示:所在地的派出所的代码;
  ⑥第17位数字表示性别:奇数表示男性,偶数表示女性;
  ⑦第18位数字是校检码:也有的说是个人信息码,一般是随计算机随机产生,用来检验身份证的正确性。校检码可以是0~9的数字,有时也用x表示。

举例: 
130503 19670401 0012这个身份证号的含义: 13为河北,05为邢台,03为桥西区,出生日期为1967年4月1日,顺序号为001,2为校验码。 
 



 

 根据身份证号(18位)提取出生年月日和计算年龄

package idcard;

import java.text.SimpleDateFormat;
import java.util.Date;

public class IdCardTest {

	//根据身份证号输出年龄
    public static int IdNOToAge(String IdNO){
        int leh = IdNO.length();
        String dates="";
        int age = 0;
        if (leh == 18) {
            dates = IdNO.substring(6, 10);
            SimpleDateFormat df = new SimpleDateFormat("yyyy");
            String year = df.format(new Date());
            age = Integer.parseInt(year)-Integer.parseInt(dates);
           
        }else {
        	System.out.println("出错!身份证长度不是18位!");
        }
        return age;
    }

    public static void main(String[] args) {
        System.out.println(IdNOToAge("120000197802150561"));
        System.out.println(IdNOToAge("32000019951110538X"));
    }
}


15位身份证号码转换成18位身份证号码

package idcard;

import java.util.Scanner;

public class IDcard15bitTo18bit {

	public static String[] trans15bitTo18bit(String[] input){
        String[] result = new String[18];
        for(int i=0;i<input.length;i++){
            if(i<=5){
                result[i] = input[i];
            }else{
                result[i+2] = input[i];
            }
        }
        //年份最后两位小于17,年份为20XX,否则为19XX
        if(Integer.valueOf(input[6])<=1&&Integer.valueOf(input[7])<=7){
            result[6]="2";
            result[7]="0";
        }else{
            result[6]="1";
            result[7]="9";
        }
        //计算最后一位
        String[] xs = {"7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2"};
        //前十七位乘以系数[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2],
        int sum = 0;
        for(int i=0;i<17;i++){
            sum+= Integer.valueOf(result[i]) * Integer.valueOf(xs[i]);
        }
        //对11求余,的余数 0 - 10 
        int rod = sum % 11;
        //所得余数映射到对应数字即可
        if(rod==0){ result[17] = "1";
        }else if(rod==1){ result[17] = "0";
        }else if(rod==2){ result[17] = "X";
        }else if(rod==3){ result[17] = "9";
        }else if(rod==4){ result[17] = "8";
        }else if(rod==5){ result[17] = "7";
        }else if(rod==6){ result[17] = "6";
        }else if(rod==7){ result[17] = "5";
        }else if(rod==8){ result[17] = "4";
        }else if(rod==9){ result[17] = "3";
        }else if(rod==10){ result[17] = "2";}

        return result;
    }
    public static void main(String[] args) {
         //创建输入对象
         Scanner sc=new Scanner(System.in);
         //获取用户输入的字符串
         String str="";
         System.out.print("请输入您的15位身份证号:");
         str=sc.nextLine();
         System.out.println("您输入的15位身份证号为:"+str);
         if(str.length()==15){
             String[] input = str.split("");
             String[] result = trans15bitTo18bit(input);
             System.out.print("您的18位身份证号是:");
             for(String c:result){
                 System.out.print(c);
             }
         }else{
             System.out.println("不符合格式的身份证号!");
         }
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_41888813/article/details/82457069