mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复DateUtil.betweenYear闰年2月问题
This commit is contained in:
parent
85b1ed2fcc
commit
849289fa55
@ -371,6 +371,7 @@ public class CalendarUtil {
|
|||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region ----- isSame
|
// region ----- isSame
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 比较两个日期是否为同一天
|
* 比较两个日期是否为同一天
|
||||||
*
|
*
|
||||||
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,16 +148,21 @@ 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) {
|
||||||
|
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月情况
|
||||||
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号
|
// 两个日期都位于2月的最后一天,此时月数按照相等对待,此时都设置为1号
|
||||||
beginCal.set(Calendar.DAY_OF_MONTH, 1);
|
beginCal.set(Calendar.DAY_OF_MONTH, 1);
|
||||||
endCal.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));
|
||||||
final long between = endCal.getTimeInMillis() - beginCal.getTimeInMillis();
|
final long between = endCal.getTimeInMillis() - beginCal.getTimeInMillis();
|
||||||
|
@ -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");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user