首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >java.time.format.DateTimeParseException:无法从TemporalAccessor获得格式的ddMMyyyyhhmmss

java.time.format.DateTimeParseException:无法从TemporalAccessor获得格式的ddMMyyyyhhmmss
EN

Stack Overflow用户
提问于 2019-03-28 10:09:16
回答 3查看 3K关注 0票数 2

我在将字符串格式化为ZonedDateTime时遇到了问题。

我的客户想要一个像ddMMyyyyhhmmss这样的格式的日期,没有分隔符或类似的东西。

这就是我迄今所做的

代码语言:javascript
运行
复制
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变量的解析过程中会发生错误:

代码语言:javascript
运行
复制
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

在堆栈溢出的搜索中,我发现对同一问题的一些回答建议使用LocalDateTime类作为中间类,然后解析到ZonedDateTime,但它仍然不起作用,引发了相同的错误。

我还尝试用以下过程更改初始化DateTimeFormatter的方式:

代码语言:javascript
运行
复制
DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("ddMMyyyyhhmmss")
                          .toFormatter()
                          .withZone(ZoneId.systemDefault());

但它仍然不起作用。我怎么才能修好它?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-03-28 10:17:34

你想:

代码语言:javascript
运行
复制
  String test = ZonedDateTime.now().format(formatter);
票数 2
EN

Stack Overflow用户

发布于 2019-03-28 10:12:55

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

票数 2
EN

Stack Overflow用户

发布于 2019-03-28 10:14:11

在解析的字符串中,没有关于TimeZone的任何信息,那么格式化程序应该如何知道如何转换为ZonedDateTime呢?

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55394931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档