Unable to obtain OffsetDateTime from TemporalAccessor

前端 未结 2 1378
抹茶落季
抹茶落季 2020-11-30 12:08

When I do this

String datum = \"20130419233512\";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(\"yyyyMMddHHmmss\").withZone(ZoneId.of(\"Europe/B         


        
2条回答
  •  旧巷少年郎
    2020-11-30 13:03

    There's no offset in your source data, and thus OffsetDateTime is not the correct type to use during parsing.

    Instead, use a LocalDateTime, since that is the type that most closely resembles the data you have. Then use atZone to assign it a time zone, and if you still need an OffsetDateTime, you can call toOffsetDateTime from there.

    String datum = "20130419233512";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    LocalDateTime datetime = LocalDateTime.parse(datum, formatter);
    ZonedDateTime zoned = datetime.atZone(ZoneId.of("Europe/Berlin"));
    OffsetDateTime result = zoned.toOffsetDateTime();
    

提交回复
热议问题