How do I format a javax.time.Instant as a string in the local time zone?

前端 未结 4 1937
迷失自我
迷失自我 2021-02-03 23:58

How do I format a javax.time.Instant as a string in the local time zone? The following translates a local Instant to UTC, not to the local time zone as I was expec

4条回答
  •  无人及你
    2021-02-04 00:48

    Try this:

    String dateTime = DateTimeFormatter.ISO_ZONED_DATE_TIME.format(
        ZonedDateTime.ofInstant(instant, ZoneId.systemDefault())
    );
    

    This gives:

    2014-08-25T21:52:07-07:00[America/Los_Angeles]
    

    You can change the format by using something other than DateTimeFormatter.ISO_ZONED_DATE_TIME as the formatter. DateTimeFormatter has a bunch of predefined formatters, or you can define your own.

提交回复
热议问题