/**
* 根据身份证号码获取年龄
* @param idNo
* @return
*/
public static int getAgeByBirth(String idNo) {
int age = 0;
try {
Date birthday = getBirthDay(idNo);
Calendar now = Calendar.getInstance();
now.setTime(new Date());// 当前时间
Calendar birth = Calendar.getInstance();
birth.setTime(birthday);
if (birth.after(now)) {//如果传入的时间,在当前时间的后面,返回0岁
age = 0;
} else {
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
int nowMonth = now.get(Calendar.MONTH);
int nowDay = now.get(Calendar.DAY_OF_MONTH);
int birthMonth = birth.get(Calendar.MONTH);
int birthDay = birth.get(Calendar.DAY_OF_MONTH);
if(age>0){
if(nowMonth < birthMonth){
return age -1;
}else if(nowMonth == birthMonth){
java根据身份证号码获取真实年龄(周岁,出生当年零岁)
最新推荐文章于 2023-11-01 16:44:36 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)