Linux 拨号vps windows公众号手机端

java怎么获取当前时间戳

lewis 8年前 (2017-12-10) 阅读数 7 #程序编程
文章标签 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
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门