diff --git a/CHANGELOG.md b/CHANGELOG.md index 294bd185f..11b94bc5e 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ * 【core 】 修复computeIfAbsent问题(issue#I5PTN3@Gitee) * 【extra 】 修复Ftp中路径问题(issue#I5R2DE@Gitee) * 【core 】 修复ConcurrentHashMap.computeIfAbsent缺陷导致的问题 +* 【core 】 修复DateUtil.parseUTC时对-的处理问题(issue#2612@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java b/hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java index e2f415285..c1659ea63 100755 --- a/hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java @@ -867,6 +867,22 @@ public class DateUtil extends CalendarUtil { // 格式类似:2018-09-13T05:34:31+08:00 return parse(utcString, DatePattern.UTC_WITH_XXX_OFFSET_FORMAT); } + } else if(ReUtil.contains("-\\d{2}:?00", utcString)){ + // Issue#2612,类似 2022-09-14T23:59:00-08:00 或者 2022-09-14T23:59:00-0800 + + // 去除类似2019-06-01T19:45:43 -08:00加号前的空格 + utcString = utcString.replace(" -", "-"); + if(':' != utcString.charAt(utcString.length() - 3)){ + utcString = utcString.substring(0, utcString.length() - 2) + ":00"; + } + + if (StrUtil.contains(utcString, CharUtil.DOT)) { + // 带毫秒,格式类似:2018-09-13T05:34:31.999-08:00 + return new DateTime(utcString, DatePattern.UTC_MS_WITH_XXX_OFFSET_FORMAT); + } else { + // 格式类似:2018-09-13T05:34:31-08:00 + return new DateTime(utcString, DatePattern.UTC_WITH_XXX_OFFSET_FORMAT); + } } else { if (length == DatePattern.UTC_SIMPLE_PATTERN.length() - 2) { // 格式类似:2018-09-13T05:34:31 diff --git a/hutool-core/src/test/java/cn/hutool/core/date/Issue2612Test.java b/hutool-core/src/test/java/cn/hutool/core/date/Issue2612Test.java new file mode 100644 index 000000000..9f285eb00 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/date/Issue2612Test.java @@ -0,0 +1,18 @@ +package cn.hutool.core.date; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Objects; + +public class Issue2612Test { + + @Test + public void parseTest(){ + Assert.assertEquals("2022-09-14 23:59:00", + Objects.requireNonNull(DateUtil.parse("2022-09-14T23:59:00-08:00")).toString()); + + Assert.assertEquals("2022-09-14 23:59:00", + Objects.requireNonNull(DateUtil.parse("2022-09-14T23:59:00-0800")).toString()); + } +}