扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

java怎么获取当前时间戳

扬州沐宇科技
2023-10-24 11:53:59
Java

在Java中,可以使用System.currentTimeMillis()方法获取当前时间戳,返回的是从1970年1月1日00:00:00 GMT到当前时间的毫秒数。另外,可以使用Instant.now().toEpochMilli()方法也可以获取当前时间戳。以下是示例代码:

public class Main {
    public static void main(String[] args) {
        // 使用System.currentTimeMillis()获取当前时间戳
        long timestamp1 = System.currentTimeMillis();
        System.out.println("当前时间戳1:" + timestamp1);

        // 使用Instant.now().toEpochMilli()获取当前时间戳
        long timestamp2 = Instant.now().toEpochMilli();
        System.out.println("当前时间戳2:" + timestamp2);
    }
}

输出结果类似于:

当前时间戳1:1627047058000
当前时间戳2:1627047058000

扫码添加客服微信