timestamp转LocalDateTime
时间: 2023-11-28 09:43:46 浏览: 154
可以使用Timestamp类的toLocalDateTime()方法将Timestamp转换为LocalDateTime。以下是一个示例代码:
```java
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
LocalDateTime localDateTime = timestamp.toLocalDateTime();
System.out.println("LocalDateTime: " + localDateTime);```
输出:
```
LocalDateTime: 2021-10-20T16:23:45.123456
```
相关问题
Timestamp转 LocalDateTime
在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
```
timestamp 转 localdatetime
可以使用Java中的Timestamp和LocalDateTime类来进行时间转换。具体方法如下:
```java
// 引入相关类
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.Instant;
// 将Timestamp转为LocalDateTime
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
LocalDateTime localDateTime = timestamp.toLocalDateTime();
// 将LocalDateTime转为Timestamp
LocalDateTime localDateTime = LocalDateTime.now();
Timestamp timestamp = Timestamp.valueOf(localDateTime);
```
其中,第一段代码将当前时间戳转为Timestamp对象,再通过toLocalDateTime()方法将其转为LocalDateTime对象;第二段代码则是将当前LocalDateTime对象转为Timestamp对象,使用valueOf()方法即可。
阅读全文