| by Arround The Web | No comments

How to Get Current Timestamp in Java

Your system clock assigns a date and timestamp to each file you create and update on your computer, an email you sent or received, and so on. Every operation of a computer depends on the date and time. Sometimes, the current date or time may be required while developing an application. Java allows you to set and adjust dates and times using two packages, java.time and java.util.

This manual will demonstrate the ways to get the current timestamp in Java.

How to Get Current Timestamp in Java?

For getting the current timestamp in Java, you can use the methods of the below-given classes:

    • Date class
    • ZonedDateTime class
    • Instant class
    • LocalDateTime class

Let’s check out the working of the methods of the mentioned classes!

Method 1: Get Current Timestamp Using Date Class

To get the current timestamp, you can use the “Date” class of the java.util package with the “SimpleDateFormat” class object to format the timestamp using the desired format.

Syntax

For getting the current timestamp using the Date Class with the SimpleDateFormat class, utilize the following syntax:

df.format(new Date());

 
Here, “df” object is a SimpleDateFormat class object that calls the “format()” method and passes a new Date class object as a parameter.

Example

In this example, we will first create an instance of the SimpleDateFormat class and pass a format that we want to use for displaying date:

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy.HH:mm:ss");

 
Then, we will create a String type variable named “timeStamp” that store the current timestamp value in the specified format:

String timeStamp = df.format(new Date());

 
Lastly, we will print the value variable timeStamp on the console window:

System.out.println("Current TimeStamp: "+ timeStamp);

 

The output shows the current timestamp in the specified format:


Let’s get the current timestamp within a specific zone.

Method 2: Get Current Timestamp Using ZonedDateTime Class

This section will demonstrate the procedure to get the current timestamp of the specific zone using the “ZonedDateTime” class. The ZonedDateTime class generates a timestamp containing zone data. The default zone of the system is obtained using the “systemDefault()” method, and the current timestamp for the given zoneId is obtained using the “now()” method.

Syntax

To get the current timestamp for the system’s zone using the “ZonedDateTime” class, use the syntax provided below:

ZonedDateTime.now(valueOfZoneId);

 
Here, the ZonedDateTime class will call the “now()” method by passing a variable of ZoneId that stores the current timestamp for the system’s zone.

Example

Firstly, we will get the current timestamp with zone information using the “systemDefault()” method of ZoneId and store it in an object named “zone”:

ZoneId zone = ZoneId.systemDefault();

 
Then, we will call the “now()” method by passing the zone as an argument. The resulting output value will be saved in the “zDateTime” object:

ZonedDateTime zDateTime = ZonedDateTime.now(zone);

 
Lastly, we will print the value of the ZonedDateTime object:

System.out.println("Current TimeStamp: "+ zDateTime);

 

The output shows the current timestamp of our zone, which is “America/Chicago”:


Let’s try another approach to get the current timestamp in Java.

Method 3: Get Current Timestamp Using Instant Class

The “Instant” class is the most common implementation of a completely unchanging instant in time. Its “now()” method is utilized to get the current timestamp in instants called seconds.

Syntax

Follow the below-given syntax to get the current timestamp using the “Instant” class:

Instant.now();

 
Example

Here, we will first create an object of the Instant class named “currentTimeStamp” that stores the value of the current timestamp by invoking the “now()” method:

Instant currentTimeStamp = Instant.now();

 
Then, print the current timestamp value using the “System.out.println()” method:

System.out.println("Current TimeStamp: "+ currentTimeStamp);

 

The output displays the current timestamp in instants, and the “T” in the output represents “Time,” which serves as a break between date and time:


Now, move towards the last method of getting the current timestamp in Java.

Method 4: Get Current Timestamp Using LocalDateTime class

In this section, we will tell you how you can get the current timestamp using the “LocalDateTime” class. You can use it with a “DateTimeFormatter” class to format it in the desired pattern. It is the most popular class for Date and time in Java.

Syntax

Follow the given syntax to use the now() method of the LocalDateTime class:

LocalDateTime.now();

 
Example

In this example, we will first create a “dateTime” object of the LocalDateTime class that stores the value of the current timestamp by invoking the “now()” method:

LocalDateTime dateTime = LocalDateTime.now();

 
Then, we will set the pattern using the “ofPattern()” method of the DateTimeFormatter class and then invokes the “format()” method by passing the “dateTime” object as an argument:

String currentTimeStamp = DateTimeFormatter.ofPattern("yyyy/MM/dd; HH:mm:ss").format(dateTime);

 
Finally, print the current timestamp on the console:

System.out.println("Current Time Stamp: "+currentTimeStamp);

 

Output


We covered various ways for getting the current timestamp in Java.

Conclusion

For getting the current timestamp in Java, you can use methods of the Date class, ZonedDateTime class, Instant class, and LocalDateTime class. These classes belong to the java.time and java.util packages. They use methods such as “now()”, “format()”, “pattern()”, and so on. In this manual, we demonstrated all the ways to get the current timestamp in Java with detailed examples.

Share Button

Source: linuxhint.com

Leave a Reply