下面一段代码给大家分享java根据身份证计算年龄的方法,具体代码如下所示:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
birthdate = idcard.substring(6,10)+"-"+idcard.substring(10,12)+"-"+idcard.substring(12,14)
public static int getagefrombirthtime(string birthtimestring){
// 先截取到字符串中的年、月、日
string strs[] = birthtimestring.trim().split("-");
int selectyear = integer.parseint(strs[0]);
int selectmonth = integer.parseint(strs[1]);
int selectday = integer.parseint(strs[2]);
// 得到当前时间的年、月、日
calendar cal = calendar.getinstance();
int yearnow = cal.get(calendar.year);
int monthnow = cal.get(calendar.month) + 1;
int daynow = cal.get(calendar.date);
// 用当前年月日减去生日年月日
int yearminus = yearnow - selectyear;
int monthminus = monthnow - selectmonth;
int dayminus = daynow - selectday;
int age = yearminus;
if (yearminus < 0) {// 选了未来的年份
age = 0;
} else if (yearminus == 0) {// 同年的,要么为1,要么为0
if (monthminus < 0) {// 选了未来的月份
age = 0;
} else if (monthminus == 0) {// 同月份的
if (dayminus < 0) {// 选了未来的日期
age = 0;
} else if (dayminus >= 0) {
age = 1;
}
} else if (monthminus > 0) {
age = 1;
}
} else if (yearminus > 0) {
if (monthminus < 0) {// 当前月>生日月
} else if (monthminus == 0) {// 同月份的,再根据日期计算年龄
if (dayminus < 0) {
} else if (dayminus >= 0) {
age = age + 1;
}
} else if (monthminus > 0) {
age = age + 1;
}
}
return age;
}
|
下面在看下java根据出生日期获得年龄
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public static int getage(date birthday) throws exception {
calendar cal = calendar.getinstance();
if (cal.before(birthday)) {
throw new illegalargumentexception(
"the birthday is before now.it's unbelievable!");
}
int yearnow = cal.get(calendar.year);
int monthnow = cal.get(calendar.month);
int dayofmonthnow = cal.get(calendar.day_of_month);
cal.settime(birthday);
int yearbirth = cal.get(calendar.year);
int monthbirth = cal.get(calendar.month);
int dayofmonthbirth = cal.get(calendar.day_of_month);
int age = yearnow - yearbirth;
if (monthnow <= monthbirth) {
if (monthnow == monthbirth) {
if (dayofmonthnow < dayofmonthbirth) age--;
}else{
age--;
}
}
system.out.println("age:"+age);
return age;
}
|
总结
以上所述是小编给大家介绍的java 根据身份证计算年龄,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:https://blog.csdn.net/HrlSnow/article/details/80266906
相关文章
猜你喜欢
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-26 81
-
2025-06-04 66
-
2025-05-29 89
-
2025-06-04 97
-
2025-05-27 39
热门评论

