温馨提示×

java怎么获取当前时间的时间戳

小亿
84
2024-08-01 16:44:07
栏目: 编程语言

在Java中获取当前时间的时间戳有多种方法,可以使用System.currentTimeMillis()方法或者使用Instant.now().toEpochMilli()方法。下面是具体的示例代码:

// 使用System.currentTimeMillis()方法获取当前时间的时间戳
long currentTimeMillis = System.currentTimeMillis();
System.out.println("当前时间的时间戳(毫秒):" + currentTimeMillis);

// 使用Instant.now().toEpochMilli()方法获取当前时间的时间戳
Instant instant = Instant.now();
long epochMilli = instant.toEpochMilli();
System.out.println("当前时间的时间戳(毫秒):" + epochMilli);

以上代码中,System.currentTimeMillis()方法返回当前时间的时间戳(毫秒),而Instant.now().toEpochMilli()方法返回当前时间的时间戳(毫秒)。两种方法都可以用来获取当前时间的时间戳。

0