【问题标题】:Parsing ZonedDateTime.parse ignoring offset解析 ZonedDateTime.parse 忽略偏移量
【发布时间】:2019-07-12 13:03:20
【问题描述】:

我正在尝试使用 dd/MM/yy HH:mm:ss zX 格式解析 22/04/17 09:24:28 UTC+01 - 但无论偏移量如何,创建的 ZonedDateTime 都是相同的。

下面是一个重现此问题的示例,我正在以编程方式更改偏移量:

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HH:mm:ss zX")
    .withZone(ZoneId.systemDefault());

System.out.println(MessageFormat.format("Current time is \"{0}\"", formatter.format(Instant.now())));

for (int i = 1; i <= 12; i++) {
    final String str = String.format("22/04/17 09:24:28 UTC+%02d", i);
    System.out.println(MessageFormat.format("Parsed String \"{0}\", got result of \"{1}\"", str,
        ZonedDateTime.parse(str, formatter)));

}

还有输出:

Current time is "12/07/19 12:59:25 ZZ"
Parsed String "22/04/17 09:24:28 UTC+01", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+02", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+03", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+04", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+05", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+06", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+07", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+08", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+09", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+10", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+11", got result of "2017-04-22T09:24:28Z[UTC]"
Parsed String "22/04/17 09:24:28 UTC+12", got result of "2017-04-22T09:24:28Z[UTC]"

请注意,无论偏移量如何,结果都是相同的。

【问题讨论】:

  • @SeanBright 如果我这样做,它根本不起作用。 Exception in thread "main" java.time.format.DateTimeParseException: Text '22/04/17 09:24:28 UTC+01' could not be parsed at index 18
  • zX 肯定不匹配 UTC+01z 匹配时区名称,例如 PSTPacific Standard TimeUTC+&lt;num&gt; 不是时区名称。 X 匹配时区偏移量,但我相信不是+01 格式。 O 格式似乎需要 UTC+01:00 而不是 UTC+01...
  • 我怀疑你想要'UTC'x
  • z 匹配 UTC,X 匹配 +01。在解析为ZonedDateTime 时,它会选择区域UTC,并忽略偏移量+01。我的猜测是解析成 OffsetDateTime 会反过来。
  • 不会将UTC+%02d 更改为+%02d 然后使用x 而不是'UTC'x 在这一点上是一样的吗?

标签: java date zoneddatetime java-time


【解决方案1】:

在您的字符串中,我将UTC+01 设为距UTC 1 小时的偏移量。因此,虽然 UTC 可能已被解释为时区(实际上不是),但这与此处无关,因为您的字符串中的时间不是 UTC,而是 UTC+01:00。所以我们不应该使用z,时区名称来解析它。这样做基本上会给你带来错误的结果(结合解析成ZonedDateTime)。

@VGR 在他/她的评论中是正确的:我们想要'UTC'x。我只使用了两个示例,足以证明它们给出了不同的结果。

    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy HH:mm:ss 'UTC'x");

    String str = "22/04/17 09:24:28 UTC+01";
    System.out.println(MessageFormat.format("Parsed String \"{0}\", got result of \"{1}\"",
            str, OffsetDateTime.parse(str, formatter)));
    str = "22/04/17 09:24:28 UTC+07";
    System.out.println(MessageFormat.format("Parsed String \"{0}\", got result of \"{1}\"",
            str, OffsetDateTime.parse(str, formatter)));

输出是:

Parsed String "22/04/17 09:24:28 UTC+01", got result of "2017-04-22T09:24:28+01:00"
Parsed String "22/04/17 09:24:28 UTC+07", got result of "2017-04-22T09:24:28+07:00"

UTC 周围的单引号表示文字文本,因此格式化程序会检查该文本是否存在,但对其没有意义。一个x 的偏移量仅包含小时(除非非零分钟是偏移量的一部分),例如+01+12。由于您的字符串包含偏移量且没有时区(例如英国时间的欧洲/伦敦),OffsetDateTime 是表示您的日期和时间的(最)正确类型。

退一步说,虽然您的格式完全是人类可读的,但它是非标准的,不适合机器解析。您可能需要考虑是否可以说服字符串的发件人为您提供 ISO 8601 格式,例如2017-04-22T09:24:28+01:00

链接:Wikipedia article: ISO 8601

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 2016-02-11
    • 2012-06-05
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    相关资源
    最近更新 更多