mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复DateUtil针对ISO8601时间格式部分场景下的解析存在问题
This commit is contained in:
@@ -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'
|
||||
|
@@ -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());
|
||||
}
|
||||
}
|
@@ -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
|
||||
|
Reference in New Issue
Block a user