修复DateUtil针对ISO8601时间格式部分场景下的解析存在问题

This commit is contained in:
Looly 2023-03-14 23:02:55 +08:00
parent 6fd8f7f8d8
commit 6f8b4dd818
4 changed files with 32 additions and 8 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.16.M1 (2023-03-12)
# 5.8.16.M1 (2023-03-14)
### 🐣新特性
* 【core 】 改进Calculator.conversion兼容乘法符号省略写法issue#2964@Github
@ -14,6 +14,7 @@
* 【poi 】 修复SXSSFWorkbook调用setComment时错位的问题issue#I6MBS5@Gitee
* 【core 】 修复BeanUtil.hasGetter没有跳过getClass方法的问题issue#I6MBS5@Gitee
* 【core 】 修复FileMagicNumber长度判断问题导致的越界异常issue#I6MACI@Gitee
* 【core 】 修复DateUtil针对ISO8601时间格式部分场景下的解析存在问题issue#2981@Github
-------------------------------------------------------------------------------------------------------------
# 5.8.15 (2023-03-09)

View File

@ -237,22 +237,22 @@ public class DatePattern {
public static final FastDateFormat JDK_DATETIME_FORMAT = FastDateFormat.getInstance(JDK_DATETIME_PATTERN, Locale.US);
/**
* UTC时间yyyy-MM-dd'T'HH:mm:ss
* ISO8601时间yyyy-MM-dd'T'HH:mm:ss
*/
public static final String UTC_SIMPLE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
/**
* UTC时间{@link FastDateFormat}yyyy-MM-dd'T'HH:mm:ss
* ISO8601时间{@link FastDateFormat}yyyy-MM-dd'T'HH:mm:ss
*/
public static final FastDateFormat UTC_SIMPLE_FORMAT = FastDateFormat.getInstance(UTC_SIMPLE_PATTERN, TimeZone.getTimeZone("UTC"));
public static final FastDateFormat UTC_SIMPLE_FORMAT = FastDateFormat.getInstance(UTC_SIMPLE_PATTERN);
/**
* UTC时间yyyy-MM-dd'T'HH:mm:ss.SSS
* ISO8601时间yyyy-MM-dd'T'HH:mm:ss.SSS
*/
public static final String UTC_SIMPLE_MS_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS";
/**
* UTC时间{@link FastDateFormat}yyyy-MM-dd'T'HH:mm:ss.SSS
* ISO8601时间{@link FastDateFormat}yyyy-MM-dd'T'HH:mm:ss.SSS
*/
public static final FastDateFormat UTC_SIMPLE_MS_FORMAT = FastDateFormat.getInstance(UTC_SIMPLE_MS_PATTERN, TimeZone.getTimeZone("UTC"));
public static final FastDateFormat UTC_SIMPLE_MS_FORMAT = FastDateFormat.getInstance(UTC_SIMPLE_MS_PATTERN);
/**
* UTC时间yyyy-MM-dd'T'HH:mm:ss'Z'

View File

@ -0,0 +1,22 @@
package cn.hutool.core.date;
import org.junit.Assert;
import org.junit.Test;
public class Issue2981Test {
/**
* https://github.com/dromara/hutool/issues/2981<br>
* 按照ISO8601规范以Z结尾表示UTC时间否则为当地时间
*/
@SuppressWarnings("DataFlowIssue")
@Test
public void parseUTCTest() {
final String str1 = "2019-01-01T00:00:00.000Z";
final String str2 = "2019-01-01T00:00:00.000";
final String str3 = "2019-01-01 00:00:00.000";
Assert.assertEquals(1546300800000L, DateUtil.parse(str1).getTime());
Assert.assertEquals(1546272000000L, DateUtil.parse(str2).getTime());
Assert.assertEquals(1546272000000L, DateUtil.parse(str3).getTime());
}
}

View File

@ -29,8 +29,9 @@ public class LocalDateTimeUtilTest {
Assert.assertNotNull(of);
Assert.assertEquals(dateStr, of.toString());
// 不加Z是标准当地时间与UTC时间不同
of = LocalDateTimeUtil.ofUTC(dt.getTime());
Assert.assertEquals(dateStr, of.toString());
Assert.assertNotEquals(dateStr, of.toString());
}
@Test