//这个程序 不是很完整 主要作用就是输入身份证号,判断身份证号是否合法 (只需要看够不够十八位,尾数是数字或者x)如果合法 输出出生年月日 性别 年龄 现在这个程序 判断尾数那有问题 计算年龄没写出来 能不能帮优化一下 把没写的写上 有错的地方改一下
package com;
import java.util.*;
public class ID {
private static Scanner scanner;
// 入口
public static void main(String[] args) {
scanner = new Scanner(System.in);
System.out.println("请输入您的身份证号:");
String idNumber = scanner.nextLine();
// 判断身份证号是否合法
Boolean idLegit = judgeId(idNumber);
if(idLegit){
String birthday = getBirthday(getStr(idNumber,6,13));
String gender = getGender(getStr(idNumber,16,16));
String age = getAge(getStr(idNumber,6,9));
System.out.println("您的生日:" + birthday);
System.out.println("您的性别:" + gender);
System.out.println("您的年龄:" + age);
}else System.out.println("身份证号有误!!!!");
}
//判定身份证号是否正确 正确的为18位 末尾为数字或者字母x
public static Boolean judgeId(String id){
if(id.length() != 18)
return false;
char[] end = {'1','2','3','4','5','6','7','8','9','0','x'};
boolean sum = true;
for(int i =0;i<end.length;i++){
if(end[i] != getStr(id,17, 17).charAt(0)){
sum = true;
}else{
sum = false;
}
}
return sum;
}
public static String getStr(String str,int a,int b){
b++;
return str.substring(a,b);
}
//提取出生年月日
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 ? "女" : "男";
}
//计算年龄,这个代码不完整
public static String getAge(String num){
String age = getStr(num,0,3);
int new_age = Integer.valueOf(age);// 强制转换
Calendar date = Calendar.getInstance();
String year = String.valueOf(date.get(Calendar.YEAR));
int new_year = Integer.valueOf(year);
return age;
}
}
// 判定身份证号是否正确 正确的为18位 末尾为数字或者字母x
public static Boolean judgeId(String id) {
if (id.length() != 18)
return false;
//取尾数检查是不是数字或者x
char lastChar = id.charAt(id.length() - 1);
if( ! (lastChar >= '0' && lastChar <= '9')//最后一位不是数字
&& lastChar != 'x' //最后一位不是小写x
&& lastChar != 'X' //最后一位不是大写X
){
return false;
}
//遍历检查前面17个是不是数字
for (int i = 0; i < id.length() - 1; i++) {
char tmp = id.charAt(i);
if(tmp < '0' || tmp > '9'){
//不是数字
return false;
}
}
return true;
}
// 计算年龄,这个代码不完整
// 这里假定年份合法
// 年龄 = 当前年份 - 身份证年份
public static String getAge(String num) {
//传进来的参数是截好的年份,不用再截了,
//第23行截的不正确应该是6,10
//6-10包括6不包括10,正好6,7,8,9截4位,你的代码截取字符串后面都少一个
//String birthdayYear = getStr(num, 6, 10);
//出生年份
int id_year = Integer.valueOf(num);// 强制转换
Calendar c = Calendar.getInstance();
//当前年份
int currYear = c.get(Calendar.YEAR);
int age = currYear - id_year;
return age + "";//int转字符串,
}
18位用字符串长度
尾号可以用ASCII码判断
出生年月这种对应字符串位置,字符串转整数
性别好像是倒是第二位男1女2
年龄获取当前时间的year-出生年份
这个程序学习还好,较真的话,要去看一下身份证的定义过程,看看身份证后4位是怎么来的,就能准确的判断这个身份证号码的真伪了。
//判定身份证号是否正确 正确的为18位 末尾为数字或者字母x
public static Boolean judgeId(String id){
if(id.length() != 18)
return false;
char[] end = {'1','2','3','4','5','6','7','8','9','0','X','x'};
boolean sum = true;
int i = 0;
for(;i<end.length;i++){
if(end[i] == getStr(id,17, 17).charAt(0)){
return true;
}
}
return false;
}
// 判断了生日 每过次生日年龄+1
public static String getAge(String num){
// 获取生日
String birthday = getBirthday(getStr(num,6,13));
//当前时间
Date currentDate = new Date();
//格式化
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
String currDate = sd.format(currentDate);
// 生日年
int bir_year = Integer.parseInt(birthday.substring(0,4));
// 当前年
int curr_year = Integer.parseInt(currDate.substring(0,4));
// 今年生日
String now_bir_str = curr_year + "-" + birthday.substring(5,10);
System.out.println(now_bir_str);
int res = now_bir_str.compareTo(currDate);
if (res > 0){
return String.valueOf(curr_year-bir_year-1);
}else {
return String.valueOf(curr_year-bir_year);
}
}
private static Scanner scanner;
// 入口
public static void main(String[] args) throws ParseException {
scanner = new Scanner(System.in);
while(true){
System.out.println("请输入您的身份证号:");
String idNumber = scanner.nextLine();
// 判断身份证号是否合法
Boolean idLegit = judgeId(idNumber);
if(idLegit){
String birthday = getBirthday(getStr(idNumber,6,13));
String gender = getGender(getStr(idNumber,16,16));
int age = getAge(birthday);
System.out.println("您的生日:" + birthday);
System.out.println("您的性别:" + gender);
System.out.println("您的年龄:" + age);
}else {
System.out.println("身份证号有误!!!!");
}
}
}
//判定身份证号是否正确 正确的为18位 末尾为数字或者字母x
public static Boolean judgeId(String id){
return id.length() == 18 && "1234567890xX".indexOf(id.charAt(17))!=-1;
}
public static String getStr(String str,int a,int b){
return str.substring(a,++b);
}
//提取出生年月日
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 = Integer.valueOf(num.charAt(0));
return gendetNum % 2 == 0 ? "女" : "男";
}
public static int getAge(String birthday) throws ParseException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date d1=sdf.parse(birthday);
Date today = new Date();
return today.getYear()- d1.getYear() + (today.getMonth()>= d1.getMonth() && today.getDate()>=d1.getDate()?1:0);
}