|
@ -675,30 +675,24 @@ public class DateUtil {
|
|
|
*/
|
|
|
public static int getAgeByBirthday(Date birthday) {
|
|
|
try {
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
if (cal.before(birthday)) {
|
|
|
return 0;
|
|
|
int age = 0;
|
|
|
|
|
|
if (birthday == null) {
|
|
|
return age;
|
|
|
}
|
|
|
int yearNow = cal.get(Calendar.YEAR);
|
|
|
int monthNow = cal.get(Calendar.MONTH) + 1;
|
|
|
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
|
cal.setTime(birthday);
|
|
|
int yearBirth = cal.get(Calendar.YEAR);
|
|
|
int monthBirth = cal.get(Calendar.MONTH) + 1;
|
|
|
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
|
int age = yearNow - yearBirth;
|
|
|
|
|
|
if (monthNow <= monthBirth) {
|
|
|
if (monthNow == monthBirth) {
|
|
|
if (dayOfMonthNow < dayOfMonthBirth) {
|
|
|
age--;
|
|
|
}
|
|
|
} else {
|
|
|
age--;
|
|
|
}
|
|
|
|
|
|
String birth = new SimpleDateFormat("yyyyMMdd").format(birthday);
|
|
|
|
|
|
int year = Integer.valueOf(birth.substring(0, 4));
|
|
|
int month = Integer.valueOf(birth.substring(4, 6));
|
|
|
int day = Integer.valueOf(birth.substring(6));
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
age = cal.get(Calendar.YEAR) - year;
|
|
|
//周岁计算
|
|
|
if (cal.get(Calendar.MONTH) > (month - 1) || (cal.get(Calendar.MONTH) == (month - 1) && cal.get(Calendar.DATE) < day)) {
|
|
|
age--;
|
|
|
}
|
|
|
|
|
|
return age;
|
|
|
} catch (Exception e) {
|
|
|
return 0;
|