Java Date and Time


Java Dates

Java does not have a built-in Date class, but we can import java.time package to work with the date and time API. The package includes multiple classes of date and time. For example:

Class Description
LocalDate Represents a date (year, month, day (yyyy-MM-dd))
LocalTime Represents a time (hour, minute, second and nanoseconds (HH-mm-ss-ns))
LocalDateTime Represents both a date and a time (yyyy-MM-dd-HH-mm-ss-ns)
DateTimeFormatter Formatter for displaying and parsing date-time objects

Display Current Date

To display the current date, enter the java.time.LocalDate class, and use its now() method:


Example
import java.time.LocalDate; // import the LocalDate class

            public class Main {
              public static void main(String[] args) {
                LocalDate myObj = LocalDate.now(); // Create a date object
                System.out.println(myObj); // Display the current date
              }
            }
            

The output will be:

2021-11-29


Display Current Time

To show current time (hour, minute, second, and second), enter the java.time.LocalTime class, and use its now() method:


Example
import java.time.LocalTime; // import the LocalTime class

            public class Main {
              public static void main(String[] args) {
                LocalTime myObj = LocalTime.now();
                System.out.println(myObj);
              }
            }
            

The output will be:

12:38:33.148516


Display Current Date and Time

To display the current date and time, enter the java.time.LocalDateTime class, and use its now() method:


Example
import java.time.LocalDateTime; // import the LocalDateTime class

            public class Main {
              public static void main(String[] args) {
                LocalDateTime myObj = LocalDateTime.now();
                System.out.println(myObj);
              }
            }
            

The output will be:

2021-11-29T12:38:33.153583


Formatting Date and Time

The "T" in the example above is used to separate a date from time. You can use the DateTimeFormatter class in the form ofPattern() in the same package to format or analyze day time items. The following example will remove both "T" and nanoseconds from date time:


Example
import java.time.LocalDateTime; // Import the LocalDateTime class
            import java.time.format.DateTimeFormatter; // Import the DateTimeFormatter class
            
            public class Main {
              public static void main(String[] args) {
                LocalDateTime myDateObj = LocalDateTime.now();
                System.out.println("Before formatting: " + myDateObj);
                DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
            
                String formattedDate = myDateObj.format(myFormatObj);
                System.out.println("After formatting: " + formattedDate);
              }
            }
            

The output will be:

Before Formatting: 2021-11-29T12:38:33.154156
After Formatting: 29-11-2021 12:38:33

The ofPattern() method accepts all types of values, if you want to display the date and time in a different way. For example:

Value Example
yyyy-MM-dd "1988-09-29"
dd/MM/yyyy "29/09/1988"
dd-MMM-yyyy "29-Sep-1988"
E, MMM dd yyyy "Thu, Sep 29 1988"