Java 8 获取时间戳:使用 Instant 类获取毫秒级时间戳
在 Java 8 中,可以使用 'Instant' 类来获取当前时间戳。'Instant' 类表示时间线上的一个特定点,它是以 UTC(协调世界时)为基准的。
以下是获取当前时间戳的示例代码:
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant timestamp = Instant.now();
System.out.println(timestamp.toEpochMilli()); // 输出毫秒级时间戳
}
}
在上面的示例中,我们首先使用 'Instant.now()' 方法获取当前的时间戳,并将其赋值给 'timestamp' 变量。然后,通过调用 'toEpochMilli()' 方法,将时间戳转换为毫秒级的时间戳,并将其打印出来。
请注意,时间戳是以 1970 年 1 月 1 日 00:00:00 UTC 为起点的。
原文地址: https://www.cveoy.top/t/topic/gFYJ 著作权归作者所有。请勿转载和采集!