在Java中,可以使用Instant
类来将时间转换为时间戳。Instant
类表示时间线上的一个特定点,可以通过Instant.now()
方法获取当前时间的Instant
对象,然后调用其toEpochMilli()
方法将时间转换为毫秒级的时间戳。
示例代码如下:
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant now = Instant.now();
long timestamp = now.toEpochMilli();
System.out.println("当前时间戳:" + timestamp);
}
}
运行以上代码,将会输出当前时间的时间戳。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:java如何将字符串转为时间戳