How To Get Current Timestamp In Java

There are multiple ways how to get current timestamp in Java. One way is to use module System and the other way is to use module Date. Unlike other languages Java provides number of miliseconds elapsed since 1 January 1970 00:00:00 UTC. Other languages provides mostly just number of seconds.

Using System

Module System provides function currentTimeMillis which returns number of miliseconds elapsed since beginning of the epoch.


long ts = System.currentTimeMillis();
System.out.println(ts);
# 1448700152

Using Date.getTime()

Module Date provides function getTime() which returns number of miliseconds elapsed since beginning of the epoch.


import java.util.Date;
// ...
long ts = (new Date()).getTime();
System.out.println(ts);
# 1448700152
This website uses cookies to ensure you get the best experience on our website. More info