根据日期计算年龄

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(4, 6));
int day = Integer.parseInt(birthday.substring(6, );

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;
}

你可能感兴趣的:(日期)