Java substring实现键入身份证信息,提取出个人信息并计算出年龄
1.说明
用户输入身份证号码,根据身份证号码,提取出归属地、出生年月、性别等信息,并计算出年龄
例如:
-
53123420230625123X
省/市:云南
性别:男
出生年月:2023年06月25日
年龄:0岁//归属地数组 static String[] ProvincesName = {"北京", "天津市", "河北", "山西", "内蒙", "辽宁", "吉林", "黑龙江", "上海", "江苏", "浙江", "安徽", "福建", "江西", "山东省", "河南", "湖北", "湖南", "广东", "广西", "海南", "重庆", "四川", "贵州", "云南", "陕西", "甘肃", "青海", "宁夏", "新疆", "台湾", "香港", "澳门"}; //归属地编码 static String[] ProvincesId = {"11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "61", "62", "63", "64", "65", "71", "81", "82"};
-
class
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;
2.实现思路
如图,为身份证号码的信息图示。因此,利用substring方法,我们便能截取到身份证号码中的每一段的信息,最后将截取到的信息进行比对循引、转换等操作,最后打印输出用户的信息。
过程:
- 利用substring方法截图到各项信息编码,然后拼接成为一个新的字符串
- 利用返回的Info字符串,针对各项信息做特定处理:
- 地区编码:与地区编码数组进行比对后返回其地名索引
- 出生年月:与单位进行拼接
- 性别:转换为整数类型,然后进行奇偶判断
- 年龄:将出生年月转换为Date类型,与当前Date进行计算
3.代码实现
StringBean类
package com.java.StringTest;
import java.util.Scanner;
public class StringBean {
private String str;
private int Digital;
public int getDigital() {
return Digital;
}
public void setDigital() {
Scanner scan =new Scanner(System.in);
Digital = scan.nextInt();
}
public StringBean() {
}
public StringBean(String str,int Digital) {
this.str = str;
this.Digital = Digital;
}
/**
* 获取
*
* @return str
*/
public String getStr() {
return str;
}
/**
* 设置
*/
public void setStr() {
Scanner scan = new Scanner(System.in);
str = scan.next();
this.str = str;
}
// public String toString() {
// return "StringBean{str = " + str + "}";
// }
}
1.信息提取
- 提取
//提取信息
public static String getInfo(String string) {
//提取归属地
String land = string.substring(0, 2);
//提取出生年月
String birthday = string.substring(6, 14);
//提取性别
String gender = string.substring(16, 17);
return land + birthday + gender;
}
- 省市查询与返回
//查询省市
public static int getProvinces(String string, String[] provincesId) {
int tem = 0;
for (int i = 0; i < provincesId.length; i++) {
if (string.substring(0, 2).equals(provincesId[i])) {
tem = i;
break;
}
}
return tem;
}
//返回省市
public static String ReProvinces(int x, String[] arr) {
return arr[x];
}
- 出生年月&性别
//出生年月
public static String getBirth(String string) {
String result = string.substring(2, 6) + "年" + string.substring(6, 8) + "月" + string.substring(8, 10) + "日";
return result;
}
//性别
public static String getGender(String string) {
int tem = Integer.parseInt(string.substring(string.length() - 1));
String gender = "";
if (tem % 2 == 0) {
gender += "女";
}
gender += "男";
return gender;
}
2.年龄计算
-
字符串转日期格式
//将字符串转换为日期格式 public static Date StrToDate(String str1) { String str = str1.substring(2, 6) + str1.substring(6, 8) + str1.substring(8, 10); //设定日期格式 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); Date date = null; try { date = format.parse(str); } catch (ParseException e) { e.printStackTrace(); } return date; }
-
计算年龄
//计算年龄 private static int getAgeByBirth(Date birthday) { int age = 0; try { Calendar now = Calendar.getInstance(); now.setTime(new Date());// 当前时间 Calendar birth = Calendar.getInstance(); birth.setTime(birthday); //如果传入的时间,在当前时间的后面,返回0岁 if (birth.after(now)) { age = 0; } else { age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR); if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) { age += 1; } } return age; } catch (Exception e) { return 0; } }
4.完整代码
package com.java.StringTest;
//Author:@Yh
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 键入身份证信息,提取出个人信息并计算出年龄
*/
public class ExtractIDInfo {
public static void main(String[] args) {
//键入身份证号码,18位
StringBean input = new StringBean();
String Id = "";
while (true) {
System.out.println("请输入您的身份证号码:");
input.setStr();
if (input.getStr().length() == 18) {
Id = input.getStr();
break;
} else {
System.out.println("无效的身份证号码!\n" + "====================");
}
}
String Info = getInfo(Id);
System.out.println("\n省/市:" + ReProvinces(getProvinces(Info, ProvincesId), ProvincesName) + "\n性别:" + getGender(Info) + "\n出生年月:" + getBirth(Info));
//年龄
System.out.println("年龄:" + getAgeByBirth(StrToDate(Info)) + "岁");
}
//归属地数组
static String[] ProvincesName = {"北京", "天津市", "河北", "山西", "内蒙", "辽宁", "吉林", "黑龙江", "上海", "江苏", "浙江", "安徽", "福建", "江西", "山东省", "河南", "湖北", "湖南", "广东", "广西", "海南", "重庆", "四川", "贵州", "云南", "陕西", "甘肃", "青海", "宁夏", "新疆", "台湾", "香港", "澳门"};
//归属地编码
static String[] ProvincesId = {"11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "61", "62", "63", "64", "65", "71", "81", "82"};
//提取信息
public static String getInfo(String string) {
//提取归属地
String land = string.substring(0, 2);
//提取出生年月
String birthday = string.substring(6, 14);
//提取性别
String gender = string.substring(16, 17);
return land + birthday + gender;
}
//查询省市
public static int getProvinces(String string, String[] provincesId) {
int tem = 0;
for (int i = 0; i < provincesId.length; i++) {
if (string.substring(0, 2).equals(provincesId[i])) {
tem = i;
break;
}
}
return tem;
}
//返回省市
public static String ReProvinces(int x, String[] arr) {
return arr[x];
}
//出生年月
public static String getBirth(String string) {
String result = string.substring(2, 6) + "年" + string.substring(6, 8) + "月" + string.substring(8, 10) + "日";
return result;
}
//性别
public static String getGender(String string) {
int tem = Integer.parseInt(string.substring(string.length() - 1));
String gender = "";
if (tem % 2 == 0) {
gender += "女";
}
else{
gender += "男";
return gender;
}
}
//将字符串转换为日期格式
public static Date StrToDate(String str1) {
String str = str1.substring(2, 6) + str1.substring(6, 8) + str1.substring(8, 10);
//设定日期格式
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
//计算年龄
private static int getAgeByBirth(Date birthday) {
int age = 0;
try {
Calendar now = Calendar.getInstance();
now.setTime(new Date());// 当前时间
Calendar birth = Calendar.getInstance();
birth.setTime(birthday);
//如果传入的时间,在当前时间的后面,返回0岁
if (birth.after(now)) {
age = 0;
} else {
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
age += 1;
}
}
return age;
} catch (Exception e) {
return 0;
}
}
}
运行展示