System.currentTimeMillis()转换成LocalDateTime
时间: 2023-11-18 21:57:08 浏览: 215
要将System.currentTimeMillis()转换成LocalDateTime,可以使用LocalDateTime的ofInstant方法。ofInstant方法需要两个参数,第一个是Instant对象,第二个是ZoneId对象。可以使用Instant.ofEpochMilli(System.currentTimeMillis())将System.currentTimeMillis()转换成Instant对象,然后再使用ZoneId.systemDefault()获取当前系统的时区,最后将这两个参数传入ofInstant方法即可得到对应的LocalDateTime对象。具体代码如下:
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.systemDefault());
另外,如果需要将时间戳转换成指定时区的LocalDateTime对象,可以使用ZoneId.of方法获取指定时区的ZoneId对象,然后将其作为ofInstant方法的第二个参数传入即可。例如,将时间戳转换成美国纽约时区的LocalDateTime对象,可以使用如下代码:
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.of("America/New_York"));
阅读全文
相关推荐


















