How to get current TimeStamp in Java

There are several ways to get the current timestamp in Java. This example works in Java6, 7 and 8.

Please note that Java 8 contains now a simpler and more efficient date and time API.

// Using the java.sql.Timestamp class
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp);

// Using the Date object
Date date = new Date();
System.out.println(new Timestamp(date.getTime()));

 

You like this content ? Share it with you entourage !