Getting Current Timestamp in Java

In Java, the timestamps are represented with the following classes:

  • java.time.Instant since Java 8
  • java.sql.Timestamp till Java 7

1. Get Current Timestamp with Instant

The Instant represents a unique point in the timeline and is primarily used to record event time-stamps in the application. It is an actual point in time, expressed using UTC – a universal time scale.

Instant instant =;
System.out.println(instant);   //2022-02-15T08:06:21.410588500Z

2. java.sql.Timestamp (Java 7 or Earlier)

This legacy class has 2 methods to get the current timestamp.

Timestamp timestamp1 = new Timestamp(System.currentTimeMillis());

Date date = new Date();
Timestamp timestamp2 = new Timestamp(date.getTime());

System.out.println(timestamp1);     //2022-02-15 13:55:56.18
System.out.println(timestamp2);     //2022-02-15 13:55:56.18	

3. Instant vs ZonedDateTime

At a high level, Instant and ZonedDateTime classes seem similar but they are not.

  • ZonedDateTime is an actual point in time but in a specific timezone.
  • Instant is a point of time in UTC.

The value of will be exactly the same in all parts of the words at a time, while the value of will be adjusted to the timezone value associated with the instance,

As a rule, consider using the Instant class for storing timestamp values in the database and passing between different applications. And use ZonedDateTime instance for displaying the information to the users in their specific timezones.

Happy Learning !!

Sourcecode Download

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment


A blog about Java and related technologies, the best practices, algorithms, and interview questions.