How to get current epoch timestamp in Java

In this article, you’ll learn how to get the current epoch timestamp in milliseconds precision in Java.

Get current timestamp in Java using 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);
    }
}

Get current timestamp in Java using the Instant class

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

    }
}