溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Java項目中根據(jù)身份證號計算年齡

發(fā)布時間:2021-02-24 16:20:41 來源:億速云 閱讀:356 作者:戴恩恩 欄目:編程語言

這篇文章主要介紹了怎么在Java項目中根據(jù)身份證號計算年齡,億速云小編覺得不錯,現(xiàn)在分享給大家,也給大家做個參考,一起跟隨億速云小編來看看吧!

Java是什么

Java是一門面向?qū)ο缶幊陶Z言,可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序。

第一代身份證:15位身份證號碼的意義

15位身份證號碼各位的含義:
1-2位省、自治區(qū)、直轄市代碼;
3-4位地級市、盟、自治州代碼;
5-6位縣、縣級市、區(qū)代碼;
7-12位出生年月日,比如670401代表1967年4月1日,這是和18位號碼的第一個區(qū)別;
13-15位為順序號,其中15位男為單數(shù),女為雙數(shù);
與18位身份證號的第二個區(qū)別沒有最后一位的校驗碼

舉例:
130503 670401 001的含義; 13為河北,05為邢臺,03為橋西區(qū),出生日期為1967年4月1日,順序號為001

第二代身份證:18位身份證號碼的意義

 ?、偾?、2位數(shù)字表示:所在省份的代碼,河南的省份代碼是41哦!
 ?、诘?、4位數(shù)字表示:所在城市的代碼;
 ?、鄣?、6位數(shù)字表示:所在區(qū)縣的代碼;
 ?、艿?~14位數(shù)字表示:出生年、月、日;
 ?、莸?5、16位數(shù)字表示:所在地的派出所的代碼;
 ?、薜?7位數(shù)字表示性別:奇數(shù)表示男性,偶數(shù)表示女性;
 ?、叩?8位數(shù)字是校檢碼:也有的說是個人信息碼,一般是隨計算機(jī)隨機(jī)產(chǎn)生,用來檢驗身份證的正確性。校檢碼可以是0~9的數(shù)字,有時也用x表示。

舉例:

130503 19670401 0012這個身份證號的含義: 13為河北,05為邢臺,03為橋西區(qū),出生日期為1967年4月1日,順序號為001,2為校驗碼。

根據(jù)身份證號(18位)提取出生年月日和計算年齡

package idcard;
import java.text.SimpleDateFormat;
import java.util.Date;
public class IdCardTest {
 //根據(jù)身份證號輸出年齡
 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位身份證號碼轉(zhuǎn)換成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"};
  //前十七位乘以系數(shù)[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求余,的余數(shù) 0 - 10
  int rod = sum % 11;
  //所得余數(shù)映射到對應(yīng)數(shù)字即可
  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) {
   //創(chuàng)建輸入對象
   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("不符合格式的身份證號!");
   }
 }
}

附:C# 版與VB 版 15位的身份證號轉(zhuǎn)為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;
}

以上就是億速云小編為大家收集整理的怎么在Java項目中根據(jù)身份證號計算年齡,如何覺得億速云網(wǎng)站的內(nèi)容還不錯,歡迎將億速云網(wǎng)站推薦給身邊好友。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI