03
Mar
2021

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

    }
}
Share

Yaniv Levy

Yaniv Levy, Entrepreneur, visioner & technology passionate with over 20 years on vast experience as a Senior Software Engineer and a Software Architect.

You may also like...