可以使用 Date 类的 getTime() 方法将 Date 类型转换为 long 类型。

示例代码如下:

Date date = new Date();
long time = date.getTime();

其中,date.getTime() 方法返回自 1970 年 1月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。

注意:Date 类已经过时,推荐使用 java.time 包中的类。例如,使用 Instant 类可以将时间戳转换为 Instant 类型,然后通过 toEpochMilli() 方法将 Instant 类型转换为 long 类型。

示例代码如下:

Instant instant = Instant.ofEpochMilli(time);
long timestamp = instant.toEpochMilli();
Java Date 类型转换为 Long 类型详解

原文地址: https://www.cveoy.top/t/topic/oGvI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录