您的位置:首页 > 其它

根据出生日期,计算年龄,精确到天

2014-03-25 11:35 405 查看
public static int calcAge(String birthday) {

int iage = 0;

if (birthday != "" || birthday != null) {

int year = Integer.parseInt(birthday.substring(0, 4));

int month = Integer.parseInt(birthday.substring(5, 7));

int day = Integer.parseInt(birthday.substring(8,10));

Calendar birthDate = new GregorianCalendar(year, month, day);

Calendar today = Calendar.getInstance();

if (today.get(Calendar.YEAR) > birthDate.get(Calendar.YEAR)) {

iage = today.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR) - 1;

if (today.get(Calendar.MONTH) + 1 > birthDate .get(Calendar.MONTH)) {

iage++;

} else if (today.get(Calendar.MONTH) + 1 == birthDate .get(Calendar.MONTH)) {

if (today.get(Calendar.DAY_OF_MONTH) >= birthDate .get(Calendar.DAY_OF_MONTH)) {

iage++;

}

}

}

return iage;

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: