Dates and Time (java.time.*)


Calculate Difference between 2 LocalDates

Use LocalDate and ChronoUnit:

LocalDate date1 = LocalDate.of(2020, 10, 15);
LocalDate date2 = LocalDate.of(2020, 10, 30);

now, since the method between of the ChronoUnit enumerator takes 2 Temporals as parameters so you can pass without a problem the LocalDate instances

LocalDate date1 = LocalDate.of(2023, Month.JANUARY, 1);
LocalDate date2 = LocalDate.of(2023, Month.MARCH, 1);
 
long differenceInDays = ChronoUnit.DAYS.between(date1, date2);
System.out.println("Difference in days: " + differenceInDays);

Date and time

Date and time without time zone information

LocalDateTime customDateTime = LocalDateTime.of(2024, Month.MARCH, 15, 12, 30);
LocalDateTime currentDateTime = LocalDateTime.now();
LocalDateTime parsedDateTime = LocalDateTime.parse("2022-09-30T18:45:00");
 
System.out.println("Custom DateTime: " + customDateTime);
System.out.println("Current DateTime: " + currentDateTime);
System.out.println("Parsed DateTime: " + parsedDateTime);

Date and time with time zone information

ZoneId customZoneId = ZoneId.of("America/New_York");
ZonedDateTime customDateTime = ZonedDateTime.of(2023, Month.JANUARY, 10, 9, 30, 0, 0, customZoneId);
 
LocalDate localDate = LocalDate.of(2023, Month.AUGUST, 15);
LocalTime localTime = LocalTime.of(14, 45);
ZonedDateTime composedDateTime = ZonedDateTime.of(localDate, localTime, customZoneId);
 
ZonedDateTime currentDateTime = ZonedDateTime.now(); // Default time zone
ZonedDateTime parsedDateTime = ZonedDateTime.parse("2023-08-15T14:45:00-05:00[America/New_York]");
 
System.out.println("Custom DateTime: " + customDateTime);
System.out.println("Composed DateTime: " + composedDateTime);
System.out.println("Current DateTime: " + currentDateTime);
System.out.println("Parsed DateTime: " + parsedDateTime);

Date and time with offset information (i.e. no DST changes taken into account)

ZoneOffset customZoneOffset = ZoneOffset.ofHours(-5);
OffsetDateTime customDateTime = OffsetDateTime.of(2024, 6, 15, 11, 0, 0, 0, customZoneOffset);
 
LocalDate localDate = LocalDate.of(2024, 12, 31);
LocalTime localTime = LocalTime.of(23, 59);
OffsetDateTime composedDateTime = OffsetDateTime.of(localDate, localTime, customZoneOffset);
 
OffsetDateTime currentDateTime = OffsetDateTime.now(); // Offset taken from the default ZoneId
OffsetDateTime parsedDateTime = OffsetDateTime.parse("2024-12-31T23:59:00-05:00");
 
System.out.println("Custom DateTime: " + customDateTime);
System.out.println("Composed DateTime: " + composedDateTime);
System.out.println("Current DateTime: " + currentDateTime);
System.out.println("Parsed DateTime: " + parsedDateTime);

Operations on dates and times

LocalDate nextWeek = LocalDate.now().plusWeeks(1);
LocalDateTime inTwoHours = LocalDateTime.now().plusHours(2);
Long daysDifference = java.time.temporal.ChronoUnit.DAYS.between(LocalDate.now(),
LocalDate.now().plusDays(5)); // 5
 
Duration durationDiff = Duration.between(Instant.now(),
ZonedDateTime.parse("2025-12-31T08:00:00+05:30[Asia/Kolkata]"));

Instant

Represents an instant in time. Can be thought of as a wrapper around a Unix timestamp.

Instant currentTime = Instant.now();
Instant epochStart1 = Instant.ofEpochMilli(1000);
Instant epochStart2 = Instant.parse("2000-01-01T00:00:00Z");
java.time.temporal.ChronoUnit.SECONDS.between(epochStart1, epochStart2); // 315532800

Usage of various classes of Date Time API

Following example also have explanation required for understanding example within it.

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.TimeZone;
 
public class DifferentMethodsExamples {
    public static void checkLocalDateTime() {
        LocalDateTime dateTimeNow = LocalDateTime.now();
        System.out.println("Current LocalDateTime: " + dateTimeNow);
 
        LocalDateTime dateTimeZone = LocalDateTime.now(ZoneId.of("Pacific/Auckland"));
        System.out.println("LocalDateTime in a different timezone: " + dateTimeZone);
 
        LocalDateTime dateTimeClock = LocalDateTime.now(Clock.system(ZoneId.of("America/Los_Angeles")));
        System.out.println("LocalDateTime using Clock: " + dateTimeClock);
 
        System.out.println("ZoneId Short IDs mapping: " + ZoneId.SHORT_IDS);
    }
 
    public static void checkLocalDate() {
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current LocalDate: " + currentDate);
 
        LocalDate dateDifferentZone = LocalDate.now(ZoneId.of("America/Panama"));
        System.out.println("LocalDate in a different timezone: " + dateDifferentZone);
    }
 
    public static void checkClock() {
        Clock utcClock = Clock.systemUTC();
        System.out.println("Time using UTC Clock: " + utcClock.instant());
    }
 
    public static void checkInstant() {
        Instant instantNow = Instant.now();
        System.out.println("Current Instant: " + instantNow);
 
        Instant instantUTC = Instant.now(Clock.systemUTC());
        System.out.println("Instant using UTC Clock: " + instantUTC);
    }
 
    public static void checkDuration() {
        System.out.println("Duration of 2 days: " + Duration.ofDays(2));
    }
 
    public static void checkLocalTime() {
        LocalTime timeNow = LocalTime.now();
        System.out.println("Current LocalTime: " + timeNow);
    }
 
    public static void checkZonedDateTime() {
        ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/Chicago"));
        System.out.println("ZonedDateTime in a different timezone: " + zonedDateTime);
    }
}

Basic Programs