请问如何根据身份证号码计算当前该人年龄(周岁)?

henbane 2005-07-06 02:09:38
如题,要求是:周岁

谢谢
...全文
4087 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
henbane 2005-07-19
  • 打赏
  • 举报
回复
判断好位数,从7至14位是此人的出生日期,用系统里的年份减去此年份,即可
==》话是这么说,但程序怎么写啊

因为系统日期里涉及的月日,怎么转换呢?又怎么和此人的出生日期比较呢/
InnocentBoy 2005-07-07
  • 打赏
  • 举报
回复
不过身份证号有15位的就要注意了。阴历阳历这个问题就不能管它了。
wwwzhigang 2005-07-07
  • 打赏
  • 举报
回复
身份证6-11位为出生年月,用当前年月去减,就得出周岁了...
tiaoci 2005-07-07
  • 打赏
  • 举报
回复
有这么简单么,身份证上的日期是可以用阴历DI~~~~
rower203 2005-07-07
  • 打赏
  • 举报
回复
先判断号位数对:
static int getAge(String IDCardNum){
int year, month, day, idLength = IDCardNum.length();
Calendar cal1 = Calendar.getInstance();
Calendar today = Calendar.getInstance();
if(idLength == 18){
year = Integer.parseInt(IDCardNum.substring(6,10));
month = Integer.parseInt(IDCardNum.substring(10,12));
day = Integer.parseInt(IDCardNum.substring(12,14));
}
else if(idLength == 15){
year = Integer.parseInt(IDCardNum.substring(6,8)) + 1900;
month = Integer.parseInt(IDCardNum.substring(8,10));
day = Integer.parseInt(IDCardNum.substring(10,12));
}
else {
System.out.println("This ID card number is invalid!");
return -1;
}
cal1.set(year, month, day);
return getYearDiff(today, cal1);
}

static int getYearDiff(Calendar cal, Calendar cal1){
int m = (cal.get(cal.MONTH)) - (cal1.get(cal1.MONTH));
int y = (cal.get(cal.YEAR)) - (cal1.get(cal1.YEAR));
return (y * 12 + m)/12;
}

public static void main(String[] args){
System.out.println(getAge("222222196406043333"));
System.out.println(getAge("222222640604333"));
System.out.println(getAge("2222226406043333"));
}
ghostsG 2005-07-07
  • 打赏
  • 举报
回复
up
yyb63915 2005-07-07
  • 打赏
  • 举报
回复
DING
mayeyun 2005-07-07
  • 打赏
  • 举报
回复
判断好位数,从7至14位是此人的出生日期,用系统里的年份减去此年份,即可
hcqhappy 2005-07-07
  • 打赏
  • 举报
回复
先判断号位数 让后在取
if123456789 2005-07-07
  • 打赏
  • 举报
回复
真牛。
成富 2005-07-07
  • 打赏
  • 举报
回复
最后一位是X很正常啊,最后一位是验证码,根据前面的数字算出来的。
qiyongjun2003 2005-07-07
  • 打赏
  • 举报
回复
还有我的号码是15位的 哈哈 别忘了判断
foxty 2005-07-07
  • 打赏
  • 举报
回复
先判断位数,然后根据位数截取不同的位置,然后判断周岁,没听说哪个身份证是用阴历而不是公历的.
stone_lee 2005-07-07
  • 打赏
  • 举报
回复
身份证包含出生日期信息的吗?判断15或者18位,取出出生日期,计算就可以了。
全粘架构师 2005-07-07
  • 打赏
  • 举报
回复
用substring()取位,再写个计算日期的类
飞翔的大麦茬 2005-07-07
  • 打赏
  • 举报
回复
不是技术问题
顶个
hmily0917 2005-07-07
  • 打赏
  • 举报
回复
为什么有的身份证(18位)最后一位是X呀?
我亲眼见过的 真的证!
rower203 2005-07-07
  • 打赏
  • 举报
回复
阴历也要按阳历处理^_^。
henbane 2005-07-06
  • 打赏
  • 举报
回复
楼上二位兄弟谢谢了。
jerry1220 2005-07-06
  • 打赏
  • 举报
回复
import java.util.Calendar;
public class Test {
public static void main(String[] args) throws Exception{
int age=getAge("110110198412200017");
System.out.println(age);
}
public static int getAge(String id){
Calendar ca =Calendar.getInstance();
int nowYear= ca.get(Calendar.YEAR);
int nowMonth= ca.get(Calendar.MONTH)+1;
int len=id.length();
if(len==18){
int IDYear=Integer.parseInt(id.substring(6,10));
int IDMonth=Integer.parseInt(id.substring(10,12));
if((IDMonth-nowMonth)>0){
return nowYear-IDYear-1;
}
else
return nowYear-IDYear;
}
else
System.out.println("错误的身份证号");
return 0;
}
}
加载更多回复(4)

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧