java timestamp转化为date
时间: 2023-10-07 15:11:23 浏览: 41
可以使用Java中的Date和SimpleDateFormat类将Timestamp转换为Date。
示例代码:
```java
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimestampToDateExample {
public static void main(String[] args) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Date date = new Date(timestamp.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(date);
System.out.println("Timestamp: " + timestamp);
System.out.println("Formatted Date: " + formattedDate);
}
}
```
输出:
```
Timestamp: 2021-10-12 16:18:32.393
Formatted Date: 2021-10-12 16:18:32
```
在上面的示例中,我们首先创建了一个Timestamp对象,然后使用其getTime()方法获取时间戳值,并将其传递给Date构造函数创建一个Date对象。接下来,我们使用SimpleDateFormat将Date对象格式化为字符串。