【问题标题】:java.time.format.DateTimeParseException: Unable to obtain ZonedDateTime from TemporalAccessor on format ddMMyyyyhhmmss [duplicate]java.time.format.DateTimeParseException:无法从格式为 ddMMyyyyhhmmss 的 TemporalAccessor 获取 ZonedDateTime [重复]
【发布时间】:2019-08-19 01:35:56
【问题描述】:

我在将字符串格式化为 ZonedDateTime 时遇到问题。
我的客户希望日期格式为 ddMMyyyyhhmmss,没有分隔符或类似的东西。
这是我到目前为止所做的

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;


public class MyClass {
    public static void main(String args[]) {

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyyhhmmss");
            String test = formatter
            .format(ZonedDateTime.now()).toString();
            System.out.println(test);
            ZonedDateTime a = ZonedDateTime.parse(test,formatter);
            System.out.println(a.toString());
    }
}

虽然它正确生成字符串,但在创建 LocalDateTime 变量的解析过程中会出现错误

28032019100707

Exception in thread "main" java.time.format.DateTimeParseException: Text '28032019100707' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
    at MyClass.main(MyClass.java:14)
Caused by: java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 of type java.time.format.Parsed
    at java.time.ZonedDateTime.from(ZonedDateTime.java:565)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    ... 2 more
Caused by: java.time.DateTimeException: Unable to obtain ZoneId from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 of type java.time.format.Parsed
    at java.time.ZoneId.from(ZoneId.java:466)
    at java.time.ZonedDateTime.from(ZonedDateTime.java:553)
    ... 4 more
Command exited with non-zero status 1

搜索 SO,我看到对同一问题的一些答案建议使用 LocalDateTime 类作为中间,然后解析为 ZonedDateTime 但它仍然无法正常工作,抛出相同的错误。 我也尝试过改变我用这个过程初始化 DateTimeFormatter 的方式

DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("ddMMyyyyhhmmss")
                          .toFormatter()
                          .withZone(ZoneId.systemDefault());

但它仍然无法正常工作。我知道我肯定错过了一些愚蠢的东西,但我看不出是什么。谁能指出我正确的方向?

【问题讨论】:

标签: java date datetime time zoneddatetime


【解决方案1】:

你想要:

  String test = ZonedDateTime.now().format(formatter);

【讨论】:

    【解决方案2】:

    ddMMyyyyhhmmss不包含任何区域信息,不是ZonedDateTime,应该是LocalDateTime

    【讨论】:

    • 我已经尝试过这种方法但仍然有同样的错误
    • @GianmarcoF。我也不明白你为什么要使用 ZonedDateTime。使用 String test = formatter.format(LocalDateTime.now()).toString(); LocalDateTime parsed = LocalDateTime.parse(test, formatter); 正在做你所要求的。
    【解决方案3】:

    在您解析的字符串中,没有关于 TimeZone 的信息 - 那么 Formatter 应该如何知道如何转换为 ZonedDateTime?

    您需要在字符串中包含 TimeZone 信息,或者使用另一个 Object 将其解析为没有 TimeZone 信息,然后将其传输到您需要的 Zone。

    【讨论】:

    • 我也尝试过使用 LocalDateTime 但我仍然收到相同的错误消息
    • @GianmarcoF。不,您没有收到相同的错误消息。当前的错误消息明确指出它缺少有关时区的信息。不管你说什么,如果你使用 LocalDateTime 就不会发生这种情况。如果您在使用 LocalDateTime 时遇到问题,请更改您的问题并显示您的确切代码和确切结果。
    • 除了没有时区信息的问题之外,它也因 LocalDateTime 而失败是正确的 - 但这是因为在 DateTimeFormatter 中使用 hh 而不是 HH(如上所述)。跨度>
    • 在这种情况下,错误是由于 hh 不是大写字母而导致错误
    猜你喜欢
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 2016-06-26
    • 2014-06-29
    • 2020-07-29
    • 1970-01-01
    • 2019-12-04
    • 2016-05-19
    相关资源
    最近更新 更多