This article will provide a profound understanding of the following essential concepts in order to work with date and time in java.
- How to import Classes of java.time package
- How to Show Current Date
- How to Show Current Time
- How to Show Current Date and Time
- How to Format Date and Time
So let’s start!
How to import Classes of java.time package
In Java, the classes of any package can be imported either individually or all classes can be imported in one go.
To import a class individually, we have to specify the class name with the java.time package as shown in the below-given snippet:
The above snippet will import a class named LocalTime of java.time package and in the same way other classes can be imported. However, if we have to import multiple classes of the same package then we can import all of them simultaneously as described in the below snippet:
Specifying a * sign with the package name indicates that import all the classes of such package.
How to Show Current Date
In java, the date can be represented in the year-month-day format using the LocalDate class.
In this example, we will use the now() method of LocalDate class to show the current date:
The complete code and respective output are shown in the below snippet:
The output verifies that the now() method displays the current date.
How to Show Current Time
In java, the time can be represented in the hours-minutes-seconds-nanoseconds format using the LocalTime class.
In this example, initially, we create the object of the LocalTime class and then utilize the now() method with the object of that class:
The output of the above code is demonstrated in the below-given screenshot:
The output authenticates the appropriateness of the above code.
How to Show Current Date and Time
In java, the time can be represented in the year-month-day-hours-minutes-seconds-nanoseconds format using the LocalDateTime class.
In this example, we avail the effectiveness of LocalDateTime class by creating the object of that class. And then we utilize the object with the now() method to display the current date and time.
The above piece of code will show the following output:
The output confirms the working of now() method along with LocalDateTime class.
How to Format Date and Time
From the above output, we noticed that time and date are separated with the letter “T”, and the date-time format is not as fruitful as it should be. Therefore to format the date and time according to our choice we can utilize the DateTimeFormatter class.
Let’s extend the previous example a little bit more, now this time we will create the object of the DateTimeFormatter class as well and we will utilize its method ofPattern().
We create an object of the LocalDateTime and utilize it with the now() method to get the current date and time. Next, we create the object of DateTimeFormatter and utilize it with the ofPattern() method. Within the ofPattern() method we specify the date-time format of our choice. Finally we utilize the format() method that will return the formatted string.
We got the date time format of our choice which authenticates the working of the above program. Similarly, you can specify the DateTime format of your choice within the ofPattern() method.
java.time package provides multiple classes such as LocalDate, LocalTime, or LocalDateTime class and all these classes have some predefined methods that can be used in order to work with date and time. For example, the now() method can be utilized with each of the above mentioned classes to get the current date, time or both current date and time. Similarly, the ofPattern() method of DateTimeFormatter class can be used to get the date and time format of the user’s choice. This write-up provides a profound understanding of how to work with date and time in java.