【每日一练】【难度中】根据某一具体出生日期计算至今多少岁

根据某一具体出生日期计算至今多少岁?

--来自网页客户端
已邀请:

sjing - Google

赞同来自: admin

 
   public static int getAgeByBirth(Date birthday) {
int age;
try {
Calendar now = Calendar.getInstance();
// 当前时间
now.setTime(new Date());
Calendar birth = Calendar.getInstance();
birth.setTime(birthday);
//如果传入的时间,在当前时间的后面,返回0岁
if (birth.before(now)) {
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
if (now.get(Calendar.DAY_OF_YEAR) < birth.get(Calendar.DAY_OF_YEAR)) {
age -= 1;
}
} else {
age = 0;
}
return age;
} catch (Exception e) {
return 0;
}
}


--来自网页客户端

要回复问题请先登录注册

Template error, template file not found