According birthday computing age, before the age of 4 months to accurate

/ ** 
* calculated according to date of birth Age
* @param BIRTHDAY
* @return
* /
public static String getAgeByBirth (a Date BIRTHDAY) {
the StringBuffer the StringBuffer new new SB = ();
the try {
int Age = 0;
int month The = 0;
Calendar Calendar CAL = .getInstance ();
IF (cal.before (BIRTHDAY)) {// birth date is later than the current time, unable to calculate
the throw new new IllegalArgumentException (
"at The BIRTHDAY IS the before Now.It's Unbelievable!");
}
int yearNow = cal.get (Calendar.YEAR); // current year
int monthNow = cal.get (Calendar.MONTH); // current month
int dayOfMonthNow = cal.get (Calendar.DAY_OF_MONTH); // current date
cal.setTime (BIRTHDAY);
int = yearBirth cal.get (Calendar.YEAR);
int = monthBirth cal.get (the Calendar.MONTH);
int = dayOfMonthBirth cal.get (Calendar.DAY_OF_MONTH);
Age = yearNow - yearBirth; / / calculated age entire
IF (monthNow <= monthBirth) {
IF (monthNow == monthBirth) {
// IF (dayOfMonthNow <dayOfMonthBirth) Age -; // current date is before birth, age Save a
} the else {
month the monthNow = - + 12 is monthBirth;
Age -; // current month before the date of birth, age, a Save
}
} the else {
month the monthNow = - monthBirth;
}

IF (Age <. 4) {
IF (Age == 0) {
sb.append(month + "个月");
} else if (month == 0) {
sb.append(age + "岁");
} else {
sb.append(age + "岁" + month + "个月");
}
} else {
sb.append(age + "岁");
}
} catch (Exception e) {
return "";
}
return sb.toString();
}

Reproduced in: https: //www.cnblogs.com/deyujincheng/p/11016433.html

Guess you like

Origin blog.csdn.net/weixin_34268753/article/details/93480397