OCA Java SE 8 Core Java APIs - Java Date Time








You need an import statement to work with the date and time classes.

Most of them are in the java.time package.

To use it, add this import to your program:

import java.time.*;          // import time classes 

Creating Dates and Times

LocalDate contains just a date-no time and no time zone, for example birthday.

LocalTime contains just a time-no date and no time zone, for example midnight.

LocalDateTime contains both a date and time but no time zone, for example midnight on New Year.

To communicate across time zones, ZonedDateTime handles them.

The following code creates new data and time objects.

System.out.println(LocalDate.now()); 
System.out.println(LocalTime.now()); 
System.out.println(LocalDateTime.now()); 

Each of the three classes has a static method called now() that gives the current date and time.

Your output depends on what date/time you run it and where you live.

Java uses T to separate the date and time when converting LocalDateTime to a String.

The following examples create the same date:

LocalDate date1 = LocalDate.of(2015, Month.JANUARY, 20); 
LocalDate date2 = LocalDate.of(2015, 1, 20); 

Both pass in the year, month, and date.

We can pass the int number of the month directly.

The method signatures are as follows:

public static LocalDate of(int year, int month, int dayOfMonth) 
public static LocalDate of(int year, Month month, int dayOfMonth) 

Month is a special type of class called an enum.

When creating a time, you can choose how detailed you want to be.

You can specify just the hour and minute, or you can add the number of seconds.

You can even add nanoseconds if you want to be very precise.

LocalTime time1 =  LocalTime.of(6, 15);               // hour and minute 
LocalTime time2 =  LocalTime.of(6, 15, 30);          // + seconds 
LocalTime time3 =  LocalTime.of(6, 15, 30, 200);     // + nanoseconds 

These three times are all different but within a minute of each other. The method signatures are as follows:

public static LocalTime of(int hour, int minute) 
public static LocalTime of(int hour, int minute, int second) 
public static LocalTime of(int hour, int minute, int second, int nanos) 

Finally, we can combine dates and times:

LocalDateTime dateTime1 = LocalDateTime.of(2015, Month.JANUARY, 20, 6, 15, 30); 
LocalDateTime dateTime2 = LocalDateTime.of(date1, time1); 

The first line of code shows how you can specify all the information about the LocalDateTime right in the same line.

The second line of code shows how you can create LocalDate and LocalTime objects separately first and then combine them to create a LocalDateTime object.

This time there are a lot of method signatures since there are more combinations.

The method signatures are as follows:

public static LocalDateTime of(int year, 
    int month, int dayOfMonth, int hour, int minute) 
public static LocalDateTime of(int year, 
    int month, int dayOfMonth, int hour, int minute, int second) 
public static LocalDateTime of(int year, 
    int month, int dayOfMonth, int hour, int minute, int second, int nanos) 
public static LocalDateTime of(int year, 
    Month month, int dayOfMonth, int hour, int minute) 
public static LocalDateTime of(int year, 
    Month month, int dayOfMonth, int hour, int minute, int second) 
public static LocalDateTime of(int year, 
    Month month, int dayOfMonth, int hour, int minute, int second, int nanos)
public static LocalDateTime of(LocalDate date, LocalTime) 

The date and time classes have private constructors to force you to use the static methods.

The following code doesn't compile.

LocalDate d = new LocalDate(); // DOES NOT COMPILE 

The following code throws exception for invalid date and time value.

LocalDate.of(2015, Month.JANUARY, 32)     // throws DateTimeException 

Creating Dates in Java 7 and Earlier





Manipulating Dates and Times

The date and time classes are immutable. We need to remember to assign the results of these methods to a reference variable so they are not lost.

LocalDate date = LocalDate.of(2014, Month.JANUARY, 20); 
System.out.println(date);          // 2014-01-20 
date = date.plusDays(2); 
System.out.println(date);          // 2014-01-22 
date = date.plusWeeks(1); 
System.out.println(date);          // 2014-01-29 
date = date.plusMonths(1); 
System.out.println(date);          // 2014-02-28 
date = date.plusYears(5); 
System.out.println(date);          // 2019-02-28 

To subtract date.

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20); 
LocalTime time = LocalTime.of(5, 15); 
LocalDateTime dateTime = LocalDateTime.of(date, time); 
System.out.println(dateTime);          // 2020-01-20T05:15 
dateTime = dateTime.minusDays(1); 
System.out.println(dateTime);          // 2020-01-19T05:15 
dateTime = dateTime.minusHours(10); 
System.out.println(dateTime);          // 2020-01-18T19:15 
dateTime = dateTime.minusSeconds(30); 
System.out.println(dateTime);          // 2020-01-18T19:14:30 

It is common for date and time methods to be chained.

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20); 
LocalTime time = LocalTime.of(5, 15); 
LocalDateTime dateTime = LocalDateTime.of(date2, time) 
   .minusDays(1).minusHours(10).minusSeconds(30); 

What do you think this prints?

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20); 
date.plusDays(10); 
System.out.println(date); 

Adding 10 days was useless because we ignored the result.

What is the output?

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20); 
date = date.plusMinutes(1);     // DOES NOT COMPILE 

LocalDate does not contain time. This means you cannot add minutes to it.

The following table lists Methods in LocalDate, LocalTime, and LocalDateTime

                         Can call on Can call on  Can call on  
                         LocalDate?  LocalTime?   LocalDateTime? 
plusYears/minusYears     Yes         No           Yes 
plusMonths/minusMonths   Yes         No           Yes 
plusWeeks/minusWeeks     Yes         No           Yes 
plusDays/minusDays       Yes         No           Yes 
plusHours/minusHours     No          Yes          Yes 
plusMinutes/minusMinutes No          Yes          Yes 
plusSeconds/minusSeconds No          Yes          Yes 
plusNanos/minusNanos     No          Yes          Yes