mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-30 12:47:58 +08:00
修复DateUtil.betweenYear闰年2月问题
This commit is contained in:
parent
85b1ed2fcc
commit
849289fa55
@ -371,6 +371,7 @@ public class CalendarUtil {
|
||||
// endregion
|
||||
|
||||
// region ----- isSame
|
||||
|
||||
/**
|
||||
* 比较两个日期是否为同一天
|
||||
*
|
||||
@ -443,8 +444,8 @@ public class CalendarUtil {
|
||||
throw new IllegalArgumentException("The date must not be null");
|
||||
}
|
||||
return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && //
|
||||
// issue#3011@Github
|
||||
cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA);
|
||||
// issue#3011@Github
|
||||
cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -647,6 +648,7 @@ public class CalendarUtil {
|
||||
}
|
||||
|
||||
// region ----- parse
|
||||
|
||||
/**
|
||||
* 通过给定的日期格式解析日期时间字符串。<br>
|
||||
* 传入的日期格式会逐个尝试,直到解析成功,返回{@link Calendar}对象,否则抛出{@link DateException}异常。
|
||||
@ -796,4 +798,26 @@ public class CalendarUtil {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -148,15 +148,20 @@ public class DateBetween implements Serializable {
|
||||
final Calendar endCal = DateUtil.calendar(end);
|
||||
|
||||
final int result = endCal.get(Calendar.YEAR) - beginCal.get(Calendar.YEAR);
|
||||
if (!isReset) {
|
||||
// 考虑闰年的2月情况
|
||||
if (Calendar.FEBRUARY == beginCal.get(Calendar.MONTH) && Calendar.FEBRUARY == endCal.get(Calendar.MONTH)) {
|
||||
if (beginCal.get(Calendar.DAY_OF_MONTH) == beginCal.getActualMaximum(Calendar.DAY_OF_MONTH)
|
||||
&& endCal.get(Calendar.DAY_OF_MONTH) == endCal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
|
||||
// 两个日期都位于2月的最后一天,此时月数按照相等对待,此时都设置为1号
|
||||
beginCal.set(Calendar.DAY_OF_MONTH, 1);
|
||||
endCal.set(Calendar.DAY_OF_MONTH, 1);
|
||||
}
|
||||
if (false == isReset) {
|
||||
final int beginMonthBase0 = beginCal.get(Calendar.MONTH);
|
||||
final int endMonthBase0 = endCal.get(Calendar.MONTH);
|
||||
if (beginMonthBase0 < endMonthBase0) {
|
||||
return result;
|
||||
} else if (beginMonthBase0 > endMonthBase0) {
|
||||
return result - 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));
|
||||
|
@ -47,6 +47,40 @@ public class DateBetweenTest {
|
||||
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
|
||||
public void betweenMonthTest() {
|
||||
final Date start = DateUtil.parse("2017-02-01 12:23:46");
|
||||
|
Loading…
Reference in New Issue
Block a user