How do I use java.time.LocalDateTime class?

The java.time.LocalDateTime class represents information of both date and time without time-zone. We can create LocalDateTime using the available static factory method such as the of() method or by combining an instance of LocalDate and LocalTime.

The following code snippet will show you both ways. First we begin with using the of() method where we can pass arguments such as the year, month, day, hour, minute and second. On the following line we also use the of() method but this time we pass an instance of LocalDate and LocalTime as the arguments.

package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;

public class LocalDateTimeDemo {
    public static void main(String[] args) {
        // Creates an instance of LocalDateTime using of()
        // static factory method with full date and time arguments.
        LocalDateTime dateTime =
                LocalDateTime.of(2021, Month.SEPTEMBER, 11, 16, 15, 15);
        System.out.println("dateTime  = " + dateTime);

        // Combines LocalDate and LocalTime to creates a new
        // instance of LocalDateTime.
        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();
        LocalDateTime dateTime2 = LocalDateTime.of(date, time);
        System.out.println("dateTime2 = " + dateTime2);

        // Creates LocalDateTime from LocateDate with time using
        // atTime() method.
        LocalDateTime dateTime3 = date.atTime(16, 15, 15);
        LocalDateTime dateTime4 = date.atTime(time);
        System.out.println("dateTime3 = " + dateTime3);
        System.out.println("dateTime4 = " + dateTime4);

        // Creates LocalDateTime from LocalTime with date using
        // atDate() method.
        LocalDateTime dateTime5 = time.atDate(date);
        System.out.println("dateTime5 = " + dateTime5);

        // Obtains LocalDate and LocalTime from LocalDateTime using
        // toLocalDate() and toLocalTime() methods.
        LocalDate date1 = dateTime5.toLocalDate();
        LocalTime time1 = dateTime5.toLocalTime();
        System.out.println("date1 = " + date1);
        System.out.println("time1 = " + time1);
    }
}

We can also create an instance of LocalDateTime by using the LocalDate‘s atTime() method or LocalTime‘s atDate() method as seen in the code snippet above.

On the very end of the code snippet you can see how to obtain a LocalDate or LocalTime information from an instance of LocalDateTime using the toLocalDate() and toLocalTime() method.

Running this code snippet will give you the following result:

dateTime  = 2021-09-11T16:15:15
dateTime2 = 2021-11-16T07:06:36.460459800
dateTime3 = 2021-11-16T16:15:15
dateTime4 = 2021-11-16T07:06:36.460459800
dateTime5 = 2021-11-16T07:06:36.460459800
date1 = 2021-11-16
time1 = 07:06:36.460459800

How do I use java.time.LocalTime class?

An instance of LocalTime class represent information about time. It doesn’t contain information about date. To create an instance of this class we can use the of() static factory method. There are two types of this method. The first one accept two arguments, hour and minute. The second type also accept the second as the arguments.

The code snippet below show you how to create an instance of LocalTime and how to obtain its values.

package org.kodejava.datetime;

import java.time.LocalTime;

public class LocalTimeDemo {
    public static void main(String[] args) {
        LocalTime time = LocalTime.of(15, 5, 30);

        int hour = time.getHour();
        int minute = time.getMinute();
        int second = time.getSecond();

        System.out.println("hour = " + hour);
        System.out.println("minute = " + minute);
        System.out.println("second = " + second);
    }
}

To get the values from the LocalTime object we can use the getHour(), getMinute() and getSecond() methods to get hour, minute and second respectively.

Running this snippet result the following output:

hour = 15
minute = 5
second = 30

You can also use the get() method to read values represented by the LocalTime object. We call this method with the temporal field that we want to read. The following code snippet will give you the same result as the previous code, only this time we use the get() method.

int hour = time.get(ChronoField.HOUR_OF_DAY);       
int minute = time.get(ChronoField.MINUTE_OF_HOUR);  
int second = time.get(ChronoField.SECOND_OF_MINUTE);

System.out.println("hour   = " + hour);             
System.out.println("minute = " + minute);           
System.out.println("second = " + second);

How do I use java.time.LocalDate class?

JDK8’s LocateDate class is a class that represent information about date. It doesn’t include information about time of day, and it also doesn’t have information about time-zone. An instance of this class is an immutable object.

To create an instance of LocateDate class we can use the of() static method factory. We pass arguments such as the year, month and day into this static factory method. The value for month can be an integer between 1 and 12, or you use the value specified by the java.time.Month enum, such as Month.JANUARY.

The code snippet below show you how to use the LocalDate class.

package org.kodejava.datetime;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;

public class LocalDateDemo {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2014, 9, 7);

        int year = date.getYear();
        Month month = date.getMonth();
        int day = date.getDayOfMonth();

        DayOfWeek dow = date.getDayOfWeek();
        int monthLength = date.lengthOfMonth();
        boolean leapYear = date.isLeapYear();

        System.out.println("Year         = " + year);
        System.out.println("Month        = " + month);
        System.out.println("Day          = " + day);
        System.out.println("Dow          = " + dow);
        System.out.println("Month Length = " + monthLength);
        System.out.println("Leap Year    = " + leapYear);
    }
}

As you can see from the code snippet above, the LocalDate class also provide some methods to get the value from the LocateDate instance. For example, you can obtain the year of the date using the getYear() method. To get the month we can use the getMonth() method which will return the Month enum. And to get the day we can use the getDayOfMonth() method.

We can also get information such as the length of the month and check if the year represented by this LocalDate is a leap year. Running the code snippet above will give you the following result:

Year         = 2014
Month        = SEPTEMBER
Day          = 7
Dow          = SUNDAY
Month Length = 30
Leap Year    = false

Beside using the methods shown above to access values from a LocalDate instance we can also use TemporalField as demonstrated in the code snippet below. Here we call the get() method and pass a temporal field that we want to read using the ChronoField enumeration. This enum implements the TemporalField interface.

int year = date.get(ChronoField.YEAR);
int month = date.get(ChronoField.MONTH_OF_YEAR);
int day = date.get(ChronoField.DAY_OF_MONTH);

System.out.println("year  = " + year);
System.out.println("month = " + month);
System.out.println("day   = " + day);

How do I get today’s date and time?

This example show you how to use the new Date and Time API introduced in JDK 8 to get the current date and time. To get today’s date in this new API you can construct an instance of LocalDate, LocalTime or LocalDateTime and call its toString() method.

All of these classes are located under the new java.time package, and defined as final classes, and because their constructor is declared as a private constructor, it means you can’t use their constructor to create a new instance. But these classes offer some static factory methods to get the value it represents or to create a new instance. For example all of these classes provide a now() method that return the current date, time or date and time information.

Let’s see a complete code snippet in the TodayDateTime example to demonstrate it.

package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class TodayDateTime {
    public static void main(String[] args) {
        // Obtains the current date from the system clock in the
        // default time-zone.
        LocalDate currentDate = LocalDate.now();
        System.out.println("currentDate = " + currentDate);

        // Obtains the current time from the system clock in the
        // default time-zone.
        LocalTime currentTime = LocalTime.now();
        System.out.println("currentTime = " + currentTime);

        // Obtains the current date-time from the system clock
        // in the default time-zone.
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("currentDateTime = " + currentDateTime);
    }
}

Running this program will give you the following result:

currentDate = 2021-11-14
currentTime = 23:14:47.694204500
currentDateTime = 2021-11-14T23:14:47.694204500