How to get the epoch timestamp in Java?

Posted on In QA

In Java, how to get the epoch timestamp, the number of seconds passed since the epoch?

In Java, you can get the current time in milliseconds and then calculate the epoch time:

long ts = System.currentTimeMillis()/1000;


$ wget --quiet -O /tmp/javarepo-428.jar && java -jar /tmp/javarepo-428.jar
Welcome to JavaREPL version 428 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25)
Type expression to evaluate, :help for more options or press tab to auto-complete.
Connected to local instance at http://localhost:33598
java> long ts = System.currentTimeMillis()/1000;
long ts = 1491474800

Leave a Reply

Your email address will not be published.