# How to get current timestamp in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 23, 2019, updated Oct 6, 2022. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-get-current-timestamp

> A quick article to learn how to get the current timestamp in Java.

To get the current timestamp in Java:

1. Use the `Instant` class from Java 8 new [date and time API](https://attacomsian.com/blog/java-new-date-time-api).
2. The `Instant.now()` static method returns an object representing a specific moment in the timeline in UTC.

```java
Instant now = Instant.now();

System.out.println(now);
// 2022-10-06T18:45:00.282494Z
```

An `Instant` is a unique point in the timeline and is mainly used to represent timestamps in Java 8 and higher. You can use this class to get the current moment from the system clock.

Alternatively, you can also convert an instance of `java.sql.Timestamp` to `Instant` as shown below:

```java
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Instant now = timestamp.toInstant();

System.out.println(now);
// 2022-10-06T19:00:03.584Z
```

Another way to create an `Instant` object is using the `java.util.Date` class:

```java
Date date = new Date();
Instant now = date.toInstant();

System.out.println(now);
// 2022-10-06T19:00:03.584Z
```

> **Note:** To get a [Unix timestamp](https://attacomsian.com/blog/java-get-unix-timestamp) (seconds from the epoch of `1970-01-01T00:00:00Z`) in Java, use the `Instant.now().getEpochSecond()` method.

In Java 7 and below, you need to use the `java.sql.Timestamp` class to get the current timestamp:

```java
// Use System.currentTimeMillis()
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// 2022-10-07 00:04:05.771

// Convert Date to Timestamp
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
```

You may be interested in reading the following Java date and time tutorials:

* [Introduction to Java 8 Date and Time API](https://attacomsian.com/blog/java-new-date-time-api)
* [How to get current date and time in Java](https://attacomsian.com/blog/java-get-current-date-time)
* [How to convert a string to date in Java](https://attacomsian.com/blog/java-convert-string-to-date)
