import java.sql.Timestamp;
import java.util.Date;
public class TimestampExample {
public static void main(String[] args) {
// 创建当前时间的Timestamp对象
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println("当前时间戳: " + timestamp);
// 将Date对象转换为Timestamp对象
Date date = new Date();
Timestamp timestampFromDate = new Timestamp(date.getTime());
System.out.println("从Date对象转换的时间戳: " + timestampFromDate);
// 获取时间戳中的秒数和纳秒数
long seconds = timestamp.getTime() / 1000;
int nanos = timestamp.getNanos();
System.out.println("秒数: " + seconds);
System.out.println("纳秒数: " + nanos);
// 设置时间戳
timestamp.setTime(1234567890000L);
System.out.println("设置后的时间戳: " + timestamp);
}
}
System.currentTimeMillis()
获取当前时间的毫秒数,并用它来创建一个Timestamp
对象。Date
对象的getTime()
方法获取毫秒数,然后用它创建Timestamp
对象。getTime()
方法获取毫秒数并转换为秒数,使用getNanos()
方法获取纳秒数。setTime()
方法设置时间戳的值。上一篇:java xms
下一篇:boolean在java中的用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站