mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix #2612
This commit is contained in:
parent
9466e7b1f6
commit
eba3332eaf
@ -67,6 +67,8 @@ import java.util.regex.Pattern;
|
||||
* <li>2022-08-23T15:20:46Z</li>
|
||||
* </ul>
|
||||
*
|
||||
* 其他格式见:<a href="https://ijmacd.github.io/rfc3339-iso8601/">https://ijmacd.github.io/rfc3339-iso8601/</a>
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class DatePattern {
|
||||
|
@ -4,6 +4,7 @@ import cn.hutool.core.date.DateException;
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.format.DefaultDateBasic;
|
||||
import cn.hutool.core.regex.ReUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
|
||||
@ -22,6 +23,7 @@ import cn.hutool.core.util.CharUtil;
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class UTCDateParser extends DefaultDateBasic implements DateParser {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static UTCDateParser INSTANCE = new UTCDateParser();
|
||||
|
||||
@ -60,6 +62,22 @@ public class UTCDateParser extends DefaultDateBasic implements DateParser {
|
||||
// 格式类似:2018-09-13T05:34:31+08:00
|
||||
return new DateTime(source, DatePattern.UTC_WITH_XXX_OFFSET_FORMAT);
|
||||
}
|
||||
} else if(ReUtil.contains("-\\d{2}:?00", source)){
|
||||
// Issue#2612,类似 2022-09-14T23:59:00-08:00 或者 2022-09-14T23:59:00-0800
|
||||
|
||||
// 去除类似2019-06-01T19:45:43 -08:00加号前的空格
|
||||
source = source.replace(" -", "-");
|
||||
if(':' != source.charAt(source.length() - 3)){
|
||||
source = source.substring(0, source.length() - 2) + ":00";
|
||||
}
|
||||
|
||||
if (StrUtil.contains(source, CharUtil.DOT)) {
|
||||
// 带毫秒,格式类似:2018-09-13T05:34:31.999-08:00
|
||||
return new DateTime(source, DatePattern.UTC_MS_WITH_XXX_OFFSET_FORMAT);
|
||||
} else {
|
||||
// 格式类似:2018-09-13T05:34:31-08:00
|
||||
return new DateTime(source, DatePattern.UTC_WITH_XXX_OFFSET_FORMAT);
|
||||
}
|
||||
} else {
|
||||
if (length == DatePattern.UTC_SIMPLE_PATTERN.length() - 2) {
|
||||
// 格式类似:2018-09-13T05:34:31
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user