mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
解决CalendarUtil.isSameDay时区不同导致结果错误问题
This commit is contained in:
parent
604be1e9b2
commit
c108cbf001
@ -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)
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user