Java

Working with Date and Time in Java | Explained

Date and time are one of the major factors in any software and to represent date and time in an effective and appropriate way is a necessity of any programming language. There are numerous ways to work with date and time in java and one of the simplest among them is to utilize the java.time package. The java.time package contains multiple classes that provide different formats for the date and time. Therefore in order to avail the functionalities of these classes, we have to import the java.time package in our project.

This article will provide a profound understanding of the following essential concepts in order to work with date and time in java.

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:

import java.time.LocalTime;

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:

import java.time.*;

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.

Example

In this example, we will use the now() method of LocalDate class to show the current date:

public static void main(String[] args) {
   LocalDate dateObject;
   dateObject = LocalDate.now();
   System.out.println("Current Date: " + dateObject);
}

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.

Example

In this example, initially, we create the object of the LocalTime class and then utilize the now() method with the object of that class:

public static void main(String[] args) {
   LocalTime timeObject;
   timeObject = LocalTime.now();
   System.out.println("Current Time: " + timeObject);  
}

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.

Example

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.

public static void main(String[] args) {
   LocalDateTime timedateObj;
   timedateObj = LocalDateTime.now();
   System.out.println("Current Date & Time: " + timedateObj);
}

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.

Example

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().

public static void main(String[] args) {
  LocalDateTime timedateObj;
  timedateObj = LocalDateTime.now();
  DateTimeFormatter dtf;
  dtf = DateTimeFormatter.ofPattern("dd-MM-yy HH:mm:ss");
  System.out.println(dtf.format(timedateObj));  
}

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.

Conclusion

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.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.