Timestamp转 LocalDateTime
时间: 2024-02-02 11:10:15 浏览: 93
在Java中,可以使用`Timestamp`类和`LocalDateTime`类相互转换。下面是一个示例代码:
```java
import java.sql.Timestamp;
import java.time.LocalDateTime;
public class TimestampToLocalDateTimeExample {
public static void main(String[] args) {
// 将Timestamp转换为LocalDateTime
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
LocalDateTime localDateTime = timestamp.toLocalDateTime();
System.out.println("Timestamp: " + timestamp);
System.out.println("LocalDateTime: " + localDateTime);
// 将LocalDateTime转换为Timestamp
LocalDateTime localDateTime2 = LocalDateTime.now();
Timestamp timestamp2 = Timestamp.valueOf(localDateTime2);
System.out.println("LocalDateTime: " + localDateTime2);
System.out.println("Timestamp: " + timestamp2);
}
}
```
输出结果:
```
Timestamp: 2021-01-01 12:34:56.789
LocalDateTime: 2021-01-01T12:34:56.789
LocalDateTime: 2021-01-01T12:34:56.789
Timestamp: 2021-01-01 12:34:56.789
```
阅读全文