mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
修复DateUtil.betweenYear闰年2月问题
This commit is contained in:
parent
f15612cd55
commit
126674a453
@ -15,6 +15,7 @@
|
|||||||
* 【core 】 修复PathMover对目标已存在且只读文件报错错误问题(issue#I95CLT@Gitee)
|
* 【core 】 修复PathMover对目标已存在且只读文件报错错误问题(issue#I95CLT@Gitee)
|
||||||
* 【json 】 修复JSONUtil序列化和反序列化预期的结果不一致问题(pr#3507@Github)
|
* 【json 】 修复JSONUtil序列化和反序列化预期的结果不一致问题(pr#3507@Github)
|
||||||
* 【http 】 修复CVE-2022-22885,HttpGlobalConfig可选关闭信任host(issue#2042@Github)
|
* 【http 】 修复CVE-2022-22885,HttpGlobalConfig可选关闭信任host(issue#2042@Github)
|
||||||
|
* 【core 】 修复DateUtil.betweenYear闰年2月问题(issue#I97U3J@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.26(2024-02-10)
|
# 5.8.26(2024-02-10)
|
||||||
|
@ -62,7 +62,7 @@ public class CalendarUtil {
|
|||||||
/**
|
/**
|
||||||
* 转换为Calendar对象
|
* 转换为Calendar对象
|
||||||
*
|
*
|
||||||
* @param millis 时间戳
|
* @param millis 时间戳
|
||||||
* @param timeZone 时区
|
* @param timeZone 时区
|
||||||
* @return Calendar对象
|
* @return Calendar对象
|
||||||
* @since 5.7.22
|
* @since 5.7.22
|
||||||
@ -356,15 +356,26 @@ 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.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) && //
|
return cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) && //
|
||||||
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && //
|
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && //
|
||||||
cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA);
|
cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否为本月最后一天
|
||||||
|
*
|
||||||
|
* @param calendar {@link Calendar}
|
||||||
|
* @return 是否为本月最后一天
|
||||||
|
* @since 5.8.27
|
||||||
|
*/
|
||||||
|
public static boolean isLastDayOfMonth(Calendar calendar) {
|
||||||
|
return calendar.get(Calendar.DAY_OF_MONTH) == calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 比较两个日期是否为同一周
|
* 比较两个日期是否为同一周
|
||||||
*
|
*
|
||||||
* @param cal1 日期1
|
* @param cal1 日期1
|
||||||
* @param cal2 日期2
|
* @param cal2 日期2
|
||||||
* @param isMon 是否为周一。国内第一天为星期一,国外第一天为星期日
|
* @param isMon 是否为周一。国内第一天为星期一,国外第一天为星期日
|
||||||
* @return 是否为同一周
|
* @return 是否为同一周
|
||||||
* @since 5.7.21
|
* @since 5.7.21
|
||||||
@ -408,9 +419,9 @@ 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) && //
|
||||||
cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) &&
|
cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) &&
|
||||||
// issue#3011@Github
|
// issue#3011@Github
|
||||||
cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA);
|
cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -657,7 +668,7 @@ public class CalendarUtil {
|
|||||||
int age = year - cal.get(Calendar.YEAR);
|
int age = year - cal.get(Calendar.YEAR);
|
||||||
|
|
||||||
//当前日期,则为0岁
|
//当前日期,则为0岁
|
||||||
if (age == 0){
|
if (age == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,14 +137,19 @@ public class DateBetween implements Serializable {
|
|||||||
|
|
||||||
int result = endCal.get(Calendar.YEAR) - beginCal.get(Calendar.YEAR);
|
int result = endCal.get(Calendar.YEAR) - beginCal.get(Calendar.YEAR);
|
||||||
if (false == 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));
|
||||||
|
@ -54,7 +54,7 @@ public class DateBetweenTest {
|
|||||||
long betweenMonth2 = new DateBetween(start2, end2).betweenMonth(false);
|
long betweenMonth2 = new DateBetween(start2, end2).betweenMonth(false);
|
||||||
Assert.assertEquals(11, betweenMonth2);
|
Assert.assertEquals(11, betweenMonth2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void betweenMinuteTest() {
|
public void betweenMinuteTest() {
|
||||||
Date date1 = DateUtil.parse("2017-03-01 20:33:23");
|
Date date1 = DateUtil.parse("2017-03-01 20:33:23");
|
||||||
@ -75,4 +75,16 @@ public class DateBetweenTest {
|
|||||||
ChronoUnit.WEEKS);
|
ChronoUnit.WEEKS);
|
||||||
Assert.assertEquals(betweenWeek, betweenWeek2);
|
Assert.assertEquals(betweenWeek, betweenWeek2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void issueI97U3JTest(){
|
||||||
|
String dateStr1 = "2024-02-29 23:59:59";
|
||||||
|
Date sdate = DateUtil.parse(dateStr1);
|
||||||
|
|
||||||
|
String dateStr2 = "2023-03-01 00:00:00";
|
||||||
|
Date edate = DateUtil.parse(dateStr2);
|
||||||
|
|
||||||
|
long result = DateUtil.betweenYear(sdate, edate, false);
|
||||||
|
Assert.assertEquals(0, result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user