DateTimeFormatterBuilder在Java 8中的用法,特别是可选选项 [英] DateTimeFormatterBuilder usages in Java 8, specifically optionals

查看:26
本文介绍了DateTimeFormatterBuilder在Java 8中的用法,特别是可选选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Joda迁移到Java 8的ZonedDateTime,但我遇到了似乎无法解决的DateTimeFormatterBuilder问题。

我想接受以下格式中的任何一种:

2013-09-20T07:00:33
2013-09-20T07:00:33.123
2013-09-20T07:00:33.123+0000
2013-09-20T07:00:33.123Z
2013-09-20T07:00:33.123Z+0000
2013-09-20T07:00:33+0000

这是我当前的构建器:

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
        .parseCaseInsensitive()
        .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
        .optionalStart()
        .appendPattern(".SSS")
        .optionalEnd()
        .optionalStart()
        .appendZoneId()
        .optionalEnd()
        .optionalStart()
        .appendPattern("Z")
        .optionalEnd()
        .toFormatter();

我可能错了,但它似乎应该与我想要的图案相匹配...对吗?

如果有人能指出我可能遗漏了什么,我将不胜感激。我也不太确定appendOffset的用法,所以如果它被证明是答案,我也会很感激的。

编辑:

Text '2013-09-20T07:00:33.061+0000' could not be parsed at index 23

查看生成器,由于可选阶段,这似乎是匹配的?

编辑2:

在看到第一个答案中的建议后,我尝试了以下内容:

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
        .parseCaseInsensitive()
        .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
        .optionalStart()
        .appendPattern(".SSS")
        .optionalEnd()
        .optionalStart()
        .appendZoneOrOffsetId()
        .optionalEnd()
        .toFormatter()

在上面的字符串上继续失败。

编辑3:

最新测试导致此异常:

java.time.format.DateTimeParseException: Text '2013-09-20T07:00:33.061+0000' could not be parsed at index 23
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:582)

推荐答案

可能是因为+0000不是区域ID,而是区域偏移量。

documentation提供此列表:

  Symbol       Meaning                     Presentation      Examples
  ------       -------                     ------------      -------
       V       time-zone ID                zone-id           America/Los_Angeles; Z; -08:30
       z       time-zone name              zone-name         Pacific Standard Time; PST
       O       localized zone-offset       offset-O          GMT+8; GMT+08:00; UTC-08:00;
       X       zone-offset 'Z' for zero    offset-X          Z; -08; -0830; -08:30; -083015; -08:30:15;
       x       zone-offset                 offset-x          +0000; -08; -0830; -08:30; -083015; -08:30:15;
       Z       zone-offset                 offset-Z          +0000; -0800; -08:00;

您可以使用appendOffset("+HHMM", "0000")(doc)或appendZoneOrOffsetId()(doc)来代替appendZoneId()

因此,您的完整格式化程序可能如下所示

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
                .optionalStart()
                .appendPattern(".SSS")
                .optionalEnd()
                .optionalStart()
                .appendZoneOrOffsetId()
                .optionalEnd()
                .optionalStart()
                .appendOffset("+HHMM", "0000")
                .optionalEnd()
                .toFormatter();
此外,创建ZonedDateTime的方式可能会影响是否存在异常。因此,我推荐以下几点,因为这一点没有任何例外。

LocalDateTime time = LocalDateTime.parse("2013-09-20T07:00:33.123+0000", formatter);
ZonedDateTime zonedTime = time.atZone(ZoneId.systemDefault());

这篇关于DateTimeFormatterBuilder在Java 8中的用法,特别是可选选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆