修复DateUtil.parseUTC时对-的处理问题

This commit is contained in:
Looly 2022-09-15 17:12:30 +08:00
parent 06fec1045c
commit 874e33458e
3 changed files with 35 additions and 0 deletions

View File

@ -20,6 +20,7 @@
* 【core 】 修复computeIfAbsent问题issue#I5PTN3@Gitee
* 【extra 】 修复Ftp中路径问题issue#I5R2DE@Gitee
* 【core 】 修复ConcurrentHashMap.computeIfAbsent缺陷导致的问题
* 【core 】 修复DateUtil.parseUTC时对-的处理问题issue#2612@Github
-------------------------------------------------------------------------------------------------------------

View File

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

View File

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