Java 8 - DateTimeFormatter and ISO_INSTANT issues with ZonedDateTime

后端 未结 4 1318
故里飘歌
故里飘歌 2020-12-13 23:59

So I would expect this code to work under the new Java 8 date/time package since all it does is to convert a given ZonedDateTime to string and back using the same built-in D

4条回答
  •  忘掉有多难
    2020-12-14 00:21

    I'm not sure, but this might be a bug in Java 8. Maybe it was intended to behave in this way, but I think that the workaround I'm going to propose to you should be the default behavior (when no ZoneId is specified, just take system default):

    ZonedDateTime now = ZonedDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT
        .withZone(ZoneId.systemDefault());
    System.out
        .println(ZonedDateTime.parse(now.format(formatter), formatter));
    

    There's a similar bug which was fixed in OpenJDK: JDK-8033662 - but it's only similar, not exactly the same.

提交回复
热议问题