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:
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:
Then, we will create a String type variable named “timeStamp” that store the current timestamp value in the specified format:
Lastly, we will print the value variable timeStamp on the console window:
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:
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”:
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:
Lastly, we will print the value of the ZonedDateTime object:
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:
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:
Then, print the current timestamp value using the “System.out.println()” method:
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:
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:
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:
Finally, print the current timestamp on the console:
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.