【问题标题】:How to parse LocalDateTime string with special pattern?如何解析具有特殊模式的 LocalDateTime 字符串?
【发布时间】:2021-07-19 19:48:13
【问题描述】:

我有一串时间,例如:2021-04-23T20:18:48.442826841Z 如何将其解析为LocalDateTime?我需要指定哪个DateTimeFormatter

【问题讨论】:

标签: java datetime java-time


【解决方案1】:
LocalDateTime.parse("2021-04-23T20:18:48.442826841Z", DateTimeFormatter.ISO_LOCAL_DATE);

为我工作。

【讨论】:

  • 它给了我 java.time.format.DateTimeParseException: Text '2021-04-23T20:18:48.442826841Z' could not be parsed, unparsed text found at index 10.
【解决方案2】:

首先,请阅读What's the difference between Instant and LocalDateTime?

参考What exactly does the T and Z mean in timestamp?,给定的字符串代表一个Instant,假设你想要一个带有系统默认时区的LocalDateTime,你可以解析如下。

LocalDateTime.ofInstant(Instant.parse("2021-04-23T20:18:48.442826841Z"), ZoneId.systemDefault());

或者DateTimeFormatter.ISO_INSTANT

LocalDateTime.parse("2021-04-23T20:18:48.442826841Z", DateTimeFormatter.ISO_INSTANT.withZone(ZoneId.systemDefault()))

【讨论】:

    猜你喜欢
    • 2011-04-11
    • 2017-07-16
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    相关资源
    最近更新 更多