How to get the epoch timestamp in Java?

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;

Example:

$ wget --quiet https://github.com/albertlatacz/java-repl/releases/download/428/javarepl-428.jar -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
java> 
Answered by dtivl.

Leave a Reply

Your email address will not be published. Required fields are marked *