温馨提示×

java如何获取当前的时间戳

小亿
89
2024-08-17 09:31:32
栏目: 编程语言

在Java中获取当前的时间戳可以使用System.currentTimeMillis()方法,该方法返回的是当前时间与1970年1月1日00:00:00 GMT之间的毫秒数。示例代码如下:

long timestamp = System.currentTimeMillis();
System.out.println("当前时间戳:" + timestamp);

另外,Java 8引入了Instant类,可以更方便地获取当前时间戳。示例如下:

Instant instant = Instant.now();
long timestamp = instant.toEpochMilli();
System.out.println("当前时间戳:" + timestamp);

这两种方法都可以获取当前的时间戳,可以根据实际情况选择使用。

0