Merge pull request #3548 from RuiGao98/v5-dev

CalendarUtil在比较同一天、同一月、同一周的时候,如果两个时区不一致会导致结果不符合预期
This commit is contained in:
Golden Looly 2024-04-16 17:22:29 +08:00 committed by GitHub
commit 604be1e9b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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;
}
}