// 获取当前时间的时间戳示例代码
// 导入必要的包
import java.time.Instant;
public class TimestampExample {
public static void main(String[] args) {
// 获取当前时间的 Instant 对象
Instant now = Instant.now();
// 将 Instant 对象转换为时间戳(以秒为单位)
long timestamp = now.getEpochSecond();
// 输出时间戳
System.out.println("当前时间的时间戳(秒): " + timestamp);
// 如果需要毫秒级时间戳,可以使用 toEpochMilli 方法
long milliTimestamp = now.toEpochMilli();
// 输出毫秒级时间戳
System.out.println("当前时间的时间戳(毫秒): " + milliTimestamp);
}
}
java.time.Instant
是 Java 8 引入的类,用于表示时间线上的一个瞬间。Instant.now()
返回当前时间的 Instant
对象。getEpochSecond()
方法返回自 Unix 纪元(1970-01-01T00:00:00Z)以来的秒数。toEpochMilli()
方法返回自 Unix 纪元以来的毫秒数。System.out.println
打印出时间戳。上一篇:java ==
下一篇:java date减一天
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站