Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.time.format.DateTimeParseException:无法从格式为DDMMYYYYHMMSS的TemporalAccessor获取ZonedDateTime_Java_Date_Datetime_Time_Zoneddatetime - Fatal编程技术网

java.time.format.DateTimeParseException:无法从格式为DDMMYYYYHMMSS的TemporalAccessor获取ZonedDateTime

java.time.format.DateTimeParseException:无法从格式为DDMMYYYYHMMSS的TemporalAccessor获取ZonedDateTime,java,date,datetime,time,zoneddatetime,Java,Date,Datetime,Time,Zoneddatetime,我无法将字符串格式化为ZoneDateTime 我的客户希望日期的格式为DDMMYYYYHMMSS,没有分隔符或类似的东西。 这就是我到目前为止所做的 import java.time.format.DateTimeFormatter; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class MyClass { public stati

我无法将字符串格式化为ZoneDateTime
我的客户希望日期的格式为DDMMYYYYHMMSS,没有分隔符或类似的东西。
这就是我到目前为止所做的

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
继续搜索,我发现对同一问题的一些答案建议使用LocalDateTime类作为中间类,然后解析到ZonedDateTime,但它仍然不起作用,抛出了相同的错误。 我还尝试使用此过程更改初始化DateTimeFormatter的方式

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

但它仍然不起作用。我知道我肯定错过了一些愚蠢的事情,但我看不出是什么。有人能给我指出正确的方向吗?

ddmmyyyhhmmss
不包含任何区域信息,它不是
ZonedDateTime
,它应该是
LocalDateTime
,在您解析的字符串中,没有关于时区的信息-那么格式化程序如何知道如何转换为ZonedDateTime

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

您需要:

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

我已经尝试过这种方法,但仍然有相同的方法error@GianmarcoF. 我也不明白你为什么要使用ZoneDateTime。使用
String test=formatter.format(LocalDateTime.now()).toString();LocalDateTime parsed=LocalDateTime.parse(测试,格式化程序)正在执行您要求的操作。我也尝试使用LocalDateTime,但仍然收到相同的错误message@GianmarcoF. 不,您没有收到相同的错误消息。当前错误消息明确指出它缺少有关时区的信息。无论你说什么,如果你使用LocalDateTime,那都不会发生。如果您在使用LocalDateTime时遇到问题,请更改您的问题,并显示准确的代码和准确的结果。除了没有时区信息的问题外,LocalDateTime也会失败是正确的,但这是因为在DateTimeFormatter(如上所述)中使用hh而不是hh。在这种情况下,错误,这是因为hh不带大写字母,这导致了与错误相关的错误:此外,在格式模式字符串中,一天中的小时需要大写字母
hh
,例如。@OleV.V。即使不能解决我的问题,你认为这有关系吗?我这样问的目的是,下次我可以避免创建一个类似的正确问题:字符串没有包含足够的信息,
ZoneDateTime
可以解析回相同的值。@OleV.V。不过,感谢您解释为什么将其标记为重复:)