修复DateUtil.betweenYear闰年2月问题

This commit is contained in:
Looly 2024-03-12 17:32:48 +08:00
parent 85b1ed2fcc
commit 849289fa55
3 changed files with 74 additions and 11 deletions

View File

@ -371,6 +371,7 @@ public class CalendarUtil {
// endregion // endregion
// region ----- isSame // region ----- isSame
/** /**
* 比较两个日期是否为同一天 * 比较两个日期是否为同一天
* *
@ -443,8 +444,8 @@ public class CalendarUtil {
throw new IllegalArgumentException("The date must not be null"); throw new IllegalArgumentException("The date must not be null");
} }
return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && // return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && //
// issue#3011@Github // issue#3011@Github
cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA); cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA);
} }
/** /**
@ -647,6 +648,7 @@ public class CalendarUtil {
} }
// region ----- parse // region ----- parse
/** /**
* 通过给定的日期格式解析日期时间字符串<br> * 通过给定的日期格式解析日期时间字符串<br>
* 传入的日期格式会逐个尝试直到解析成功返回{@link Calendar}对象否则抛出{@link DateException}异常 * 传入的日期格式会逐个尝试直到解析成功返回{@link Calendar}对象否则抛出{@link DateException}异常
@ -796,4 +798,26 @@ public class CalendarUtil {
return age; return age;
} }
/**
* 是否为本月第一天
*
* @param calendar {@link Calendar}
* @return 是否为本月最后一天
* @since 6.0.0
*/
public static boolean isFirstDayOfMonth(final Calendar calendar) {
return 1 == calendar.get(Calendar.DAY_OF_MONTH);
}
/**
* 是否为本月最后一天
*
* @param calendar {@link Calendar}
* @return 是否为本月最后一天
* @since 5.8.27
*/
public static boolean isLastDayOfMonth(final Calendar calendar) {
return calendar.get(Calendar.DAY_OF_MONTH) == calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
} }

View File

@ -148,15 +148,20 @@ public class DateBetween implements Serializable {
final Calendar endCal = DateUtil.calendar(end); final Calendar endCal = DateUtil.calendar(end);
final int result = endCal.get(Calendar.YEAR) - beginCal.get(Calendar.YEAR); final int result = endCal.get(Calendar.YEAR) - beginCal.get(Calendar.YEAR);
if (!isReset) { if (false == isReset) {
// 考虑闰年的2月情况 final int beginMonthBase0 = beginCal.get(Calendar.MONTH);
if (Calendar.FEBRUARY == beginCal.get(Calendar.MONTH) && Calendar.FEBRUARY == endCal.get(Calendar.MONTH)) { final int endMonthBase0 = endCal.get(Calendar.MONTH);
if (beginCal.get(Calendar.DAY_OF_MONTH) == beginCal.getActualMaximum(Calendar.DAY_OF_MONTH) if (beginMonthBase0 < endMonthBase0) {
&& endCal.get(Calendar.DAY_OF_MONTH) == endCal.getActualMaximum(Calendar.DAY_OF_MONTH)) { return result;
// 两个日期都位于2月的最后一天此时月数按照相等对待此时都设置为1号 } else if (beginMonthBase0 > endMonthBase0) {
beginCal.set(Calendar.DAY_OF_MONTH, 1); return result - 1;
endCal.set(Calendar.DAY_OF_MONTH, 1); } else if (Calendar.FEBRUARY == beginMonthBase0
} && CalendarUtil.isLastDayOfMonth(beginCal)
&& CalendarUtil.isLastDayOfMonth(endCal)) {
// 考虑闰年的2月情况
// 两个日期都位于2月的最后一天此时月数按照相等对待此时都设置为1号
beginCal.set(Calendar.DAY_OF_MONTH, 1);
endCal.set(Calendar.DAY_OF_MONTH, 1);
} }
endCal.set(Calendar.YEAR, beginCal.get(Calendar.YEAR)); endCal.set(Calendar.YEAR, beginCal.get(Calendar.YEAR));

View File

@ -47,6 +47,40 @@ public class DateBetweenTest {
Assertions.assertEquals(18, betweenYear); Assertions.assertEquals(18, betweenYear);
} }
@Test
public void betweenYearTest3(){
final String dateStr1 = "2023-02-28 00:00:01";
final Date sdate = DateUtil.parse(dateStr1);
final String dateStr2 = "2024-02-29 00:00:00";
final Date edate = DateUtil.parse(dateStr2);
final long result = DateUtil.betweenYear(sdate, edate, false);
Assertions.assertEquals(0, result);
}
@Test
public void betweenYearTest4(){
final String dateStr1 = "2024-02-29 00:00:00";
final Date sdate = DateUtil.parse(dateStr1);
final String dateStr2 = "2025-02-28 00:00:00";
final Date edate = DateUtil.parse(dateStr2);
final long result = DateUtil.betweenYear(sdate, edate, false);
Assertions.assertEquals(1, result);
}
@Test
public void issueI97U3JTest(){
final String dateStr1 = "2024-02-29 23:59:59";
final Date sdate = DateUtil.parse(dateStr1);
final String dateStr2 = "2023-03-01 00:00:00";
final Date edate = DateUtil.parse(dateStr2);
final long result = DateUtil.betweenYear(sdate, edate, false);
Assertions.assertEquals(0, result);
}
@Test @Test
public void betweenMonthTest() { public void betweenMonthTest() {
final Date start = DateUtil.parse("2017-02-01 12:23:46"); final Date start = DateUtil.parse("2017-02-01 12:23:46");