JAVA 时间戳

时间戳(Timestamp)是一个表示特定时间点的数值,通常指的是自某个固定的起始时间(如1970年1月1日00:00:00 UTC)以来经过的秒数或毫秒数。

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


long timestamp = System.currentTimeMillis(); System.out.println(timestamp);

除了 System.currentTimeMillis() 方法外,还可以使用 Instant 类、LocalDateTime 类等来操作时间戳。例如,使用 Instant.now() 获取当前时间的时间戳:


import java.time.Instant; Instant instant = Instant.now();
long timestamp = instant.toEpochMilli(); 
System.out.println(timestamp);

需要注意的是,时间戳是一个数字,它代表了一个特定的时间点,可以用于各种时间相关的计算和比较。在不同的编程语言和系统中,时间戳的单位和精度可能有所不同,因此在处理时间戳时需要注意单位的转换和精度的处理。

你可能感兴趣的:(TOOL_Teacat,java,开发语言)