import java.sql.Timestamp;
import java.text.SimpleDateFormat;
public class TimestampToStringExample {
public static void main(String[] args) {
// 创建一个Timestamp对象,当前时间戳
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// 定义日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 将Timestamp转换为String
String timestampString = dateFormat.format(timestamp);
// 输出结果
System.out.println("Timestamp: " + timestamp);
System.out.println("Converted String: " + timestampString);
}
}
new Timestamp(System.currentTimeMillis())
用于获取当前时间的时间戳。SimpleDateFormat
类来定义输出的日期格式(例如:"yyyy-MM-dd HH:mm:ss")。dateFormat.format(timestamp)
方法将 Timestamp
对象转换为指定格式的字符串。Timestamp
和转换后的字符串表示。上一篇:java mock
下一篇:java int范围
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站