Java — How to get current epoch timestamp | Code Factory

Donate : Link

WordPress Blog : Link

Get current timestamp in Java using System.currentTimeMillis()

package com.example.java.programming.datetime;/**
* @author code.factory
*
*/
public class Test {
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);
}
}

Output :

Current epoch timestamp in millis: 1593423347845

Get current timestamp in Java using the Instant class

package com.example.java.programming.datetime;import java.time.Instant;/**
* @author code.factory
*
*/
public class Test {
public static void main(String... args) {
// Get current timestamp using Instant
long currentTimestamp = Instant.now().toEpochMilli();
System.out.println("Current epoch timestamp in millis: " + currentTimestamp);
}
}

Output :

Current epoch timestamp in millis: 1593423516547

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store