Java — How to get current epoch timestamp | Code Factory
1 min readJun 29, 2020
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