java timestamp 转String
时间: 2023-10-13 11:23:40 浏览: 178
可以使用 `java.text.SimpleDateFormat` 类来将 `java.sql.Timestamp` 转换为字符串。以下是一个示例:
```java
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
public class TimestampToStringExample {
public static void main(String[] args) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strTimestamp = sdf.format(timestamp);
System.out.println(strTimestamp);
}
}
```
此代码将当前时间戳转换为字符串格式,格式为 `yyyy-MM-dd HH:mm:ss`。您可以根据需要更改日期格式。
相关问题
java Timestamp 怎么转String
可以使用 `SimpleDateFormat` 类将 `Timestamp` 转换为 `String`。以下是示例代码:
```
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = sdf.format(timestamp);
```
在上面的代码中, `timestamp` 是一个 `Timestamp` 对象, `sdf` 是一个 `SimpleDateFormat` 对象,用于格式化日期和时间的模式。最后, `strDate` 变量包含了 `timestamp` 转换为字符串后的结果。
timestamp转string
以下是将Timestamp转化为String的示例代码:
```java
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp now = new Timestamp(System.currentTimeMillis());
String str = df.format(now);
System.out.println(str);
```
这段代码中,我们首先创建了一个SimpleDateFormat对象,指定了日期时间的格式为"yyyy-MM-dd HH:mm:ss"。然后,我们使用Timestamp类的构造函数获取当前系统时间的Timestamp对象。接下来,我们使用SimpleDateFormat的format方法将Timestamp对象转化为String类型的时间字符串。最后,我们打印输出这个时间字符串。
阅读全文
相关推荐
















