如何在Java中获得精确到毫秒的当前纪元时间戳

60 阅读1分钟

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

How to get current epoch timestamp in Java

在这篇文章中,你将学习如何在Java中获得精确到毫秒的当前纪元时间戳。

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

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);
    }
}

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

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);

    }
}