java 毫秒 localdatetime
时间: 2023-09-25 16:14:54 浏览: 151
要将毫秒转换为LocalDateTime对象,可以使用java.time.Instant类和java.time.LocalDateTime类的静态方法。以下是一个示例代码:
```java
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
long milliseconds = 1627597432000L; // 毫秒值
// 使用Instant类将毫秒转换为LocalDateTime对象
LocalDateTime dateTime = Instant.ofEpochMilli(milliseconds)
.atZone(ZoneOffset.systemDefault())
.toLocalDateTime();
System.out.println("LocalDateTime: " + dateTime);
}
}
```
在上述代码中,我们使用`Instant.ofEpochMilli(milliseconds)`将毫秒值转换为Instant对象。然后,使用`.atZone(ZoneOffset.systemDefault())`将Instant对象转换为系统默认时区的ZonedDateTime对象。最后,使用`.toLocalDateTime()`将ZonedDateTime对象转换为LocalDateTime对象。
运行上述代码,将输出转换后的LocalDateTime对象。根据给定的毫秒值,你会得到相应的日期和时间。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)