From c108cbf00152829d433b61d4e1a9944dd87dd9ab Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 16 Apr 2024 17:28:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3CalendarUtil.isSameDay?= =?UTF-8?q?=E6=97=B6=E5=8C=BA=E4=B8=8D=E5=90=8C=E5=AF=BC=E8=87=B4=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++- .../cn/hutool/core/date/CalendarUtil.java | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca9b8a06c..0122c7cb1 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.28(2024-04-15) +# 5.8.28(2024-04-16) ### 🐣新特性 * 【core 】 修正XmlUtil的omitXmlDeclaration描述注释(issue#I9CPC7@Gitee) @@ -16,6 +16,7 @@ * 【core 】 修复FileMagicNumber.getMagicNumber空指针问题(issue#I9FE8B@Gitee) * 【extra 】 修复CompressUtil工具多出\问题(issue#I71K5V@Gitee) * 【db 】 解决oracle情况下setObject(inputStream)报错问题,java.sql.SQLException: 无效的列类型问题(pr#1207@Gitee) +* 【core 】 解决CalendarUtil.isSameDay时区不同导致结果错误问题(pr#3548@Github) ------------------------------------------------------------------------------------------------------------- # 5.8.27(2024-03-29) diff --git a/hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java b/hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java index 477b12d09..2c133e18f 100644 --- a/hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java @@ -5,6 +5,7 @@ import cn.hutool.core.convert.NumberChineseFormatter; import cn.hutool.core.date.format.DateParser; import cn.hutool.core.date.format.FastDateParser; import cn.hutool.core.date.format.GlobalCustomFormat; +import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; @@ -356,9 +357,10 @@ public class CalendarUtil { throw new IllegalArgumentException("The date must not be null"); } - // 统一时区 - cal1 = toDefaultTimeZone(cal1); - cal2 = toDefaultTimeZone(cal2); + if(ObjUtil.notEqual(cal1.getTimeZone(), cal2.getTimeZone())){ + // 统一时区 + cal2 = changeTimeZone(cal2, cal1.getTimeZone()); + } return cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) && // cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && // @@ -424,9 +426,10 @@ public class CalendarUtil { throw new IllegalArgumentException("The date must not be null"); } - // 统一时区 - cal1 = toDefaultTimeZone(cal1); - cal2 = toDefaultTimeZone(cal2); + if(ObjUtil.notEqual(cal1.getTimeZone(), cal2.getTimeZone())){ + // 统一时区 + cal2 = changeTimeZone(cal2, cal1.getTimeZone()); + } return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && // cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && @@ -805,10 +808,10 @@ public class CalendarUtil { * @param cal 时间 * @return 默认时区的calendar对象 */ - private static Calendar toDefaultTimeZone(Calendar cal) { + private static Calendar changeTimeZone(Calendar cal, TimeZone timeZone) { // 转换到统一时区,例如UTC cal = (Calendar) cal.clone(); - cal.setTimeZone(TimeZone.getDefault()); + cal.setTimeZone(timeZone); return cal; } }