Unable to obtain OffsetDateTime from TemporalAccessor

前端 未结 2 1351
抹茶落季
抹茶落季 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 12:44

    The problem is that there is a difference between what a ZoneId is and a ZoneOffset is. To create a OffsetDateTime, you need an zone offset. But there is no one-to-one mapping between a ZoneId and a ZoneOffset because it actually depends on the current daylight saving time. For the same ZoneId like "Europe/Berlin", there is one offset for summer and a different offset for winter.

    For this case, it would be easier to use a ZonedDateTime instead of an OffsetDateTime. During parsing, the ZonedDateTime will correctly be set to the "Europe/Berlin" zone id and the offset will also be set according to the daylight saving time in effect for the date to parse:

    public static void main(String[] args) {
        String datum = "20130419233512";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneId.of("Europe/Berlin"));
        ZonedDateTime datetime = ZonedDateTime.parse(datum, formatter);
    
        System.out.println(datetime.getZone()); // prints "Europe/Berlin"
        System.out.println(datetime.getOffset()); // prints "+02:00" (for this time of year)
    }
    

    Note that if you really want an OffsetDateTime, you can use ZonedDateTime.toOffsetDateTime() to convert a ZonedDateTime into an OffsetDateTime.

    0 讨论(0)
  • 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();
    
    0 讨论(0)
提交回复
热议问题