温馨提示×

java怎么获取当前时间戳

小亿
75
2023-10-24 11:53:59
栏目: 编程语言

在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

0