学堂 学堂 学堂公众号手机端

在Java中,可以使用Instant类来将时间转换为时间戳。Instant类表示时间线上的一个特定点,可以通过Instant.now()方法获取当前时间的Instant对象,然后调用其toEpochMilli()方法将时间转换为毫秒级的时间戳。

lewis 2年前 (2023-10-09) 阅读数 5 #技术

在Java中,可以使用Instant类来将时间转换为时间戳。Instant类表示时间线上的一个特定点,可以通过Instant.now()方法获取当前时间的Instant对象,然后调用其toEpochMilli()方法将时间转换为毫秒级的时间戳。

示例代码如下:

importjava.time.Instant; publicclassMain{ publicstaticvoidmain(String[]args){ Instantnow=Instant.now(); longtimestamp=now.toEpochMilli(); System.out.println("当前时间戳:"+timestamp); } }

运行以上代码,将会输出当前时间的时间戳。


版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门