如何在 Java 中获取当前时间戳

x33g5p2x  于2021-10-16 转载在 Java  
字(0.6k)|赞(0)|评价(0)|浏览(436)

在本文中,您将学习如何在 Java 中获取以毫秒为单位的当前纪元时间戳。

使用 System.currentTimeMillis() 在 Java 中获取当前时间戳

public class CurrentEpochTimestampExample {
    public static void main(String[] args) {
        // Get epoch timestamp using System.currentTimeMillis()
        long currentTimestamp = System.currentTimeMillis();
        System.out.println("Current epoch timestamp in millis: " + currentTimestamp);
    }
}

使用 Instant 类获取 Java 中的当前时间戳

import java.time.Instant;

public class CurrentEpochTimestampExample {
    public static void main(String[] args) {
        // Get current timestamp using Instant
        currentTimestamp = Instant.now().toEpochMilli();
        System.out.println("Current epoch timestamp in millis: " + currentTimestamp);

    }
}

相关文章

微信公众号

最新文章

更多