How do I use java.time.Instant class of Java Date-Time API?

The java.time.Instant class in the Java Date-Time API is an immutable representation of a point in time. It stores a long count of seconds from the epoch of the first moment of 1970 UTC, plus a number of nanoseconds for the further precision within that second.

The java.time.LocalDate class represents a date without a time or time zone. It is used to represent just a date as year-month-day (e.g., 2023-03-27) in the ISO-8601 calendar system.

The java.time.LocalTime class represents a time without a date or time zone. It is used to represent just a time as hour-minute-second (e.g., 13:45:20).

It’s also worth noting that Instant class is part of Java 8’s new date and time API which was brought in to address the shortcomings of the old java.util.Date and java.util.Calendar API.

Here’s a quick example of how to use the Instant class:

package org.kodejava.datetime;

import java.time.Instant;

public class InstantExample {
    public static void main(String[] args) {
        // Get the current point in time
        Instant now = Instant.now();
        System.out.println("Current time: " + now);

        // Add duration of 500 seconds from now
        Instant later = now.plusSeconds(500);
        System.out.println("500 seconds later: " + later);

        // Subtract duration of 500 seconds from now
        Instant earlier = now.minusSeconds(500);
        System.out.println("500 seconds earlier: " + earlier);

        // Compare two Instants
        int comparison = now.compareTo(later);
        if (comparison < 0) {
            System.out.println("Now is earlier than later");
        } else if (comparison > 0) {
            System.out.println("Now is later than later");
        } else {
            System.out.println("Now and later are at the same time");
        }
    }
}

Output:

Current time: 2024-01-18T09:26:56.152268Z
500 seconds later: 2024-01-18T09:35:16.152268Z
500 seconds earlier: 2024-01-18T09:18:36.152268Z
Now is earlier than later

In this example, Instant.now() is used to get the current Instant. Various methods like plusSeconds(), minusSeconds(), and compareTo() are used to manipulate and compare the Instant.

LocalDate and LocalTime are local in the sense that they represent date and time from the context of the observer, without a time zone.

To connect Instant with LocalDate and LocalTime, you need a time zone. This is because Instant is in UTC and LocalDate/LocalTime are in a local time zone, so you need to explicitly provide a conversion between them.

Here’s how you convert an Instant to a LocalDate and a LocalTime:

package org.kodejava.datetime;

import java.time.*;

public class InstantConvertExample {
    public static void main(String[] args) {
        Instant now = Instant.now();
        System.out.println("Instant: " + now);

        // Get the system default timezone
        ZoneId zoneId = ZoneId.systemDefault(); 

        LocalDate localDate = now.atZone(zoneId).toLocalDate();
        System.out.println("LocalDate: " + localDate);

        LocalTime localTime = now.atZone(zoneId).toLocalTime();
        System.out.println("LocalTime: " + localTime);
    }
}

Here Instant.now() gives the current timestamp. .atZone(ZoneId.systemDefault()) converts it to ZonedDateTime which is then converted to LocalDate and LocalTime by using .toLocalDate() and .toLocalTime() respectively.

You can also go from LocalDate and LocalTime back to Instant. Here’s how:

package org.kodejava.datetime;

import java.time.*;

public class ToInstantExample {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        ZoneId zoneId = ZoneId.systemDefault();
        Instant instantFromDateAndTime = LocalDateTime.of(localDate, localTime).atZone(zoneId).toInstant();

        System.out.println("Instant from LocalDate and LocalTime: " + instantFromDateAndTime);
    }
}

How do I use atDate() method of Java Date-Time API?

The atDate() method is a part of LocalTime class in the Java Date-Time API. This method combines this time with a date to create an instance of LocalDateTime.

Here’s an example:

package org.kodejava.datetime;

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

public class AtDateExample {
    public static void main(String[] args) {
        // Create a LocalTime instance
        LocalTime time = LocalTime.of(14, 20);

        // Create a LocalDate instance
        LocalDate date = LocalDate.of(2023, 1, 23);

        // Using atDate to combine time and date into a LocalDateTime
        LocalDateTime dateTime = time.atDate(date);

        System.out.println(dateTime);
    }
}

Output:

2023-01-23T14:20

In this example, a LocalTime and a LocalDate are combined into a LocalDateTime using the atDate() method. This method is useful when you have a LocalTime instance and want to combine it with a date. It’s in some sense a converse operation to LocalDate‘s atTime().

How do I use atTime() method of Java Date-Time API?

The atTime() method belongs to the LocalDate class in the Java Date-Time API, not the Date class. This method combines this date with a time to create a LocalDateTime.

Here’s an example:

package org.kodejava.datetime;

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

public class AtTimeExample {
    public static void main(String[] args) {
        // Create a LocalDate instance
        LocalDate date = LocalDate.of(2023, 1, 23);

        // Create a LocalTime instance
        // 24-hour clocks
        LocalTime time = LocalTime.of(13, 45);

        // Use atTime() to combine date and time into a LocalDateTime
        LocalDateTime dateTime = date.atTime(time);

        System.out.println(dateTime);
    }
}

Output:

2023-01-23T13:45

In this example, a LocalDate and a LocalTime are combined into a LocalDateTime using the atTime() method.

The LocalDate class also has an overloaded atTime method that takes the hour and minute directly, instead of a LocalTime instance.

Here’s an example where we set 14 hours and 30 minutes directly.

package org.kodejava.datetime;

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

public class AtTimeOtherExample {
    public static void main(String[] args) {
        // Create a LocalDate instance
        LocalDate date = LocalDate.of(2023, 1, 23);

        // Use atTime() to combine date and hour and minute into a
        // LocalDateTime
        LocalDateTime dateTime = date.atTime(14, 30);

        System.out.println(dateTime);
    }
}

Output:

2023-01-23T14:30

In this example, 14:30 (in 24-hour clock) is directly passed into atTime. There is another version of atTime() that takes hrs, min, and sec. That would look like this:

// include seconds
LocalDateTime dateTime = date.atTime(14, 30, 20);
// include nano seconds
LocalDateTime dateTime = date.atTime(14, 30, 20, 200); 

These are all the overloaded versions of atTime() in LocalDate.

Remember, LocalDate, LocalTime, LocalDateTime and others from Java Date-Time API are designed to replace the old Date and Calendar classes from java.util package. They are more consistent, easier to understand and use.

How do I modified the value of LocalDate and LocalTime object?

The easiest way to modify the value of a LocalDate, LocalTime or LocalDateTime object is to use the with() method of the corresponding object. These methods will return a modified version of the object, it doesn’t change the attribute of the original object. All the methods, like withYear(), withDayOfMonth() or the with(ChronoField) of the LocalDate object will return a new object with the modified attribute.

With the LocalTime object you can use the withHour(), withMinute(), withSecond() or the more generic with(ChronoField) method to modified the attribute of a LocalTime object. You can also modified a LocalDateTime object using these with() method. Let’s see the example in the code snippet below.

package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;

public class ManipulatingDateTime {
    public static void main(String[] args) {
        LocalDate date1 = LocalDate.of(2021, 4, 21);
        System.out.println("date1 = " + date1);
        LocalDate date2 = date1.withYear(2020);
        System.out.println("date2 = " + date2);
        LocalDate date3 = date2.withDayOfMonth(10);
        System.out.println("date3 = " + date3);
        LocalDate date4 = date3.with(ChronoField.MONTH_OF_YEAR, 12);
        System.out.println("date4 = " + date4);

        LocalTime time1 = LocalTime.of(1, 5, 10);
        System.out.println("time1 = " + time1);
        LocalTime time2 = time1.withHour(6);
        System.out.println("time2 = " + time2);
        LocalTime time3 = time2.withMinute(45);
        System.out.println("time3 = " + time3);
        LocalTime time4 = time3.with(ChronoField.SECOND_OF_MINUTE, 25);
        System.out.println("time4 = " + time4);

        LocalDate now1 = LocalDate.now();
        System.out.println("now1 = " + now1);
        LocalDate now2 = now1.plusWeeks(1);
        System.out.println("now2 = " + now2);
        LocalDate now3 = now2.minusMonths(2);
        System.out.println("now3 = " + now3);
        LocalDate now4 = now3.plus(15, ChronoUnit.DAYS);
        System.out.println("now4 = " + now4);
    }
}

The output of this code snippet are:

date1 = 2021-04-21
date2 = 2020-04-21
date3 = 2020-04-10
date4 = 2020-12-10
time1 = 01:05:10
time2 = 06:05:10
time3 = 06:45:10
time4 = 06:45:25
now1 = 2021-11-22
now2 = 2021-11-29
now3 = 2021-09-29
now4 = 2021-10-14

These with() methods is the counterpart of the get() methods. Where the get() methods will give you the value of the corresponding LocalDate or LocalTime attribute, the with() method will change the attribute value and return a new object. It didn’t call set because the object is immutable, which means it value cannot be changed.

While with the with() method you can change the value of date time attribute in an absolute way using the plus() or minus() method can help you change the date and time attribute in a relative way. The plus() and minus() method allows you to move a Temporal back or forward a give amount of time, defined by a number plus a TemporalUnit, in this case we use the ChronoUnit enumeration which implements this interface.

How do I use TemporalField to access date time value?

The LocalDate and LocalTime are probably the first two classes from the Java 8 Date and Time API that you will work with. An instance of the LocalDate object is an immutable object representing a date without the time of the day and on the other way around the LocalTime object is an immutable object representing a time without the date information.

The LocalDate object have methods to get information related to date such as getYear(), getMonth(), getDayOfMonth(). While the LocalTime object have methods to get information related to time such as getHour(), getMinute(), getSecond(). Beside using those methods we can also access the value of these object using the TemporalField interface. We can pass a TemporalField to the get() method of LocalDate and LocalTime objects. TemporalField is an interface, one of its implementation that we can use to get the value is the ChronoField enumerations.

Let’s see some examples in the code snippet below:

package org.kodejava.datetime;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoField;

public class DateTimeValueTemporalField {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
        System.out.println("Date = " + date);
        System.out.println("Year = " + date.getYear());
        System.out.println("Year = " + date.get(ChronoField.YEAR));

        System.out.println("Month= " + date.getMonth().getValue());
        System.out.println("Month= " + date.get(ChronoField.MONTH_OF_YEAR));

        System.out.println("Date = " + date.getDayOfMonth());
        System.out.println("Date = " + date.get(ChronoField.DAY_OF_MONTH));

        System.out.println("DOW  = " + date.getDayOfWeek().getValue());
        System.out.println("DOW  = " + date.get(ChronoField.DAY_OF_WEEK) + "\n");

        LocalTime time = LocalTime.now();
        System.out.println("Time  = " + time);
        System.out.println("Hour  = " + time.getHour());
        System.out.println("Hour  = " + time.get(ChronoField.HOUR_OF_DAY));

        System.out.println("Minute= " + time.getMinute());
        System.out.println("Minute= " + time.get(ChronoField.MINUTE_OF_HOUR));

        System.out.println("Second= " + time.getSecond());
        System.out.println("Second= " + time.get(ChronoField.SECOND_OF_MINUTE));

        System.out.println("Nano  = " + time.getNano());
        System.out.println("Nano  = " + time.get(ChronoField.NANO_OF_SECOND));
    }
}

The output of the code snippet above are:

Date = 2021-11-22
Year = 2021
Year = 2021
Month= 11
Month= 11
Date = 22
Date = 22
DOW  = 1
DOW  = 1

Time  = 10:52:18.082348200
Hour  = 10
Hour  = 10
Minute= 52
Minute= 52
Second= 18
Second= 18
Nano  = 82348200
Nano  = 82348200