From 0658f28d68566c74bb9cc1ddfe664bd4f97b6ae6 Mon Sep 17 00:00:00 2001 From: gaorui <1484351027@qq.com> Date: Tue, 16 Apr 2024 09:59:36 +0800 Subject: [PATCH] =?UTF-8?q?CalendarUtil=E5=9C=A8=E6=AF=94=E8=BE=83?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E5=A4=A9=E3=80=81=E5=90=8C=E4=B8=80=E6=9C=88?= =?UTF-8?q?=E3=80=81=E5=90=8C=E4=B8=80=E5=91=A8=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=EF=BC=8C=E5=A6=82=E6=9E=9C=E4=B8=A4=E4=B8=AA=E6=97=B6=E5=8C=BA?= =?UTF-8?q?=E4=B8=8D=E4=B8=80=E8=87=B4=E4=BC=9A=E5=AF=BC=E8=87=B4=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E4=B8=8D=E7=AC=A6=E5=90=88=E9=A2=84=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/date/CalendarUtil.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) 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 a44d8b95a..477b12d09 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 @@ -355,6 +355,11 @@ public class CalendarUtil { if (cal1 == null || cal2 == null) { throw new IllegalArgumentException("The date must not be null"); } + + // 统一时区 + cal1 = toDefaultTimeZone(cal1); + cal2 = toDefaultTimeZone(cal2); + return cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) && // cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && // cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA); @@ -418,6 +423,11 @@ public class CalendarUtil { if (cal1 == null || cal2 == null) { throw new IllegalArgumentException("The date must not be null"); } + + // 统一时区 + cal1 = toDefaultTimeZone(cal1); + cal2 = toDefaultTimeZone(cal2); + return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && // cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && // issue#3011@Github @@ -788,4 +798,17 @@ public class CalendarUtil { return parser.parse(StrUtil.str(str), new ParsePosition(0), calendar) ? calendar : null; } + + /** + * 转换为默认时区的Calendar + * + * @param cal 时间 + * @return 默认时区的calendar对象 + */ + private static Calendar toDefaultTimeZone(Calendar cal) { + // 转换到统一时区,例如UTC + cal = (Calendar) cal.clone(); + cal.setTimeZone(TimeZone.getDefault()); + return cal; + } }