How to get the epoch timestamp in Java?

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

asked May 23, 2017 by dtivl (2,500 points)

1 Answer

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 May 27, 2017 by dtivl (2,500 points)

Please log in or register to answer this question.

Related questions

1 answer
asked May 23, 2017 by dtivl (2,500 points)
1 answer
1 answer
asked May 23, 2017 by dtivl (2,500 points)
1 answer
asked May 23, 2017 by dtivl (2,500 points)
1 answer
asked May 23, 2017 by dtivl (2,500 points)
1 answer
asked May 23, 2017 by dtivl (2,500 points)
1 answer
asked May 23, 2017 by dtivl (2,500 points)
1 answer
asked May 23, 2017 by dtivl (2,500 points)
Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...