Java根据身份证号计算年龄,15位身份证号码转18位原理与操作示例_百度文 ...

Java根据⾝份证号计算年龄,15位⾝份证号码转18位原理
与操作⽰例
本⽂实例讲述了Java根据⾝份证号计算年龄,15位⾝份证号码转18位。分享给⼤家供⼤家参考,具体如下:
第⼀代⾝份证: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;
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("120000************"));
System.out.println(IdNOToAge("320000************"));
}
}
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位⾝份证号:");
Line();
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("不符合格式的⾝份证号!");
}
}
}
附:C# 版与VB 版 15位的⾝份证号转为18位操作⽰例
C# 版
function ID15T18(strTemp)
{
var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
var nTemp = 0, i;
if(strTemp.length==15)
{
strTemp = strTemp.substr(0,6) + '19' + strTemp.substr(6,strTemp.length-6);
for(i = 0; i < strTemp.length; i ++)
{
nTemp += strTemp.substr(i, 1) * arrInt[i];
}
strTemp += arrCh[nTemp % 11];
}
return strTemp;
}
VB 版
privatestringConvert15To18(stringstrTemp)
{
int[]arrInt=newint[]{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
stringarrCh="10X98765432";
intnTemp=0;
if(strTemp.Length==15)
{
strTemp=strTemp.Substring(0,6)+"19"+strTemp.Substring(6,strTemp.Length-6);
for(inti=0;i<strTemp.Length;i++)
{
nTemp+=int.Parse(strTemp.Substring(i,1).ToString())*arrInt[i];
}
strTemp+=arrCh[nTemp%11];
}
chardd=arrCh[nTemp%11];
returnstrTemp;
}
PS:这⾥再提供⼀款本站⾝份证归属地信息查询⼯具供⼤家参考:
另外,本站在线⼯具⼩程序上也有⼀款功能更加强⼤的⾝份证信息获取⼯具,感兴趣的朋友可以扫描如下⼩程序码查看:更多关于java相关内容感兴趣的读者可查看本站专题:《》、《》、《》和《》
希望本⽂所述对⼤家java程序设计有所帮助。

本文发布于:2024-09-20 12:30:44,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/819924.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:代码   份证   证号   数字   感兴趣   年龄   本站
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议