|
|
@@ -1711,6 +1711,16 @@ public class DateUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 是否闰年
|
|
|
+ *
|
|
|
+ * @param year 年
|
|
|
+ * @return 是否闰年
|
|
|
+ */
|
|
|
+ public static boolean isLeapYear(int year) {
|
|
|
+ return new GregorianCalendar().isLeapYear(year);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 计算相对于dateToCompare的年龄,长用于计算指定生日在某年的年龄
|
|
|
*
|
|
|
* @param birthDay 生日
|
|
|
@@ -1725,17 +1735,20 @@ public class DateUtil {
|
|
|
throw new IllegalArgumentException(StrUtil.format("Birthday is after date {}!", formatDate(dateToCompare)));
|
|
|
}
|
|
|
|
|
|
- int year = cal.get(Calendar.YEAR);
|
|
|
- int month = cal.get(Calendar.MONTH);
|
|
|
- int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
|
|
|
+ final int year = cal.get(Calendar.YEAR);
|
|
|
+ final int month = cal.get(Calendar.MONTH);
|
|
|
+ final int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
|
|
|
+ final boolean isLastDayOfMonth = dayOfMonth == cal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
|
|
|
|
cal.setTime(birthDay);
|
|
|
int age = year - cal.get(Calendar.YEAR);
|
|
|
|
|
|
- int monthBirth = cal.get(Calendar.MONTH);
|
|
|
+ final int monthBirth = cal.get(Calendar.MONTH);
|
|
|
if (month == monthBirth) {
|
|
|
- int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
|
|
|
- if (dayOfMonth < dayOfMonthBirth) {
|
|
|
+
|
|
|
+ final int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
|
|
|
+ final boolean isLastDayOfMonthBirth = dayOfMonthBirth == cal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
|
+ if ((false == isLastDayOfMonth || false == isLastDayOfMonthBirth) && dayOfMonth < dayOfMonthBirth) {
|
|
|
// 如果生日在当月,但是未达到生日当天的日期,年龄减一
|
|
|
age--;
|
|
|
}
|
|
|
@@ -1748,16 +1761,6 @@ public class DateUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 是否闰年
|
|
|
- *
|
|
|
- * @param year 年
|
|
|
- * @return 是否闰年
|
|
|
- */
|
|
|
- public static boolean isLeapYear(int year) {
|
|
|
- return new GregorianCalendar().isLeapYear(year);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* 判定给定开始时间经过某段时间后是否过期
|
|
|
*
|
|
|
* @param startDate 开始时间
|