How can I convert a time in milliseconds to ZonedDateTime

前端 未结 4 567
忘了有多久
忘了有多久 2021-01-13 03:18

I have the time in milliseconds and I need to convert it to a ZonedDateTime object.

I have the following code

long m = System.currentTimeMillis();
L         


        
4条回答
  •  太阳男子
    2021-01-13 03:38

    You can construct a ZonedDateTime from an instant (this uses the system zone ID):

    //Instant is time-zone unaware, the below will convert to the given zone
    ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(m), 
                                    ZoneId.systemDefault());
    

    And if you need a LocalDateTime instance from that:

    //And this date-time will be "local" to the above zone
    LocalDateTime ldt = zdt.toLocalDateTime();
    

提交回复
热议问题