// 获取当前时间戳的示例代码
import java.time.Instant;
public class CurrentTimestamp {
public static void main(String[] args) {
// 获取当前时间戳(以秒为单位)
long currentTimestampInSeconds = Instant.now().getEpochSecond();
System.out.println("当前时间戳(秒): " + currentTimestampInSeconds);
// 获取当前时间戳(以毫秒为单位)
long currentTimestampInMilliseconds = System.currentTimeMillis();
System.out.println("当前时间戳(毫秒): " + currentTimestampInMilliseconds);
}
}
Instant.now().getEpochSecond()
:获取当前时间戳,以秒为单位。Instant
类表示时间线上的一个瞬时点,getEpochSecond()
方法返回自 1970-01-01T00:00:00Z 以来的秒数。System.currentTimeMillis()
:获取当前时间戳,以毫秒为单位。这是 Java 中获取时间戳的常用方法,返回自 1970-01-01T00:00:00Z 以来的毫秒数。这两种方法都可以用来获取当前的时间戳,具体使用哪种取决于你的需求。
上一篇:java 获取当前日期0点
下一篇:java string分割成数组
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站