00:00

Date and Time API in Java 8

Last updated: · Standard (Java 8)

The java.time package, added in Java 8 (JEP 150), is the modern Date and Time API. It replaces most uses of java.util.Date and Calendar with clear, immutable and thread-safe types such as LocalDate, ZonedDateTime and Instant.

This tutorial shows which class to use for which job, how to calculate with dates, and how to work across time zones.

1. Why Was a New Date and Time API Needed?

The old classes had well-known problems. Date is mutable, so a date shared between threads can change unexpectedly. Months in Calendar start at 0, SimpleDateFormat is not thread-safe, and there was no clean way to represent "a date without a time" or "a duration of 3 hours".

java.time, based on the experience of the Joda-Time library, fixes these problems with immutable value types and separate classes for separate concepts.

2. Choosing the Right Class

ClassRepresentsExample value
LocalDatea date without time or zone2026-11-27
LocalTimea time of day15:30
LocalDateTimedate and time, no zone2026-11-27T15:30
ZonedDateTimedate and time in a zone2026-01-15T01:40+05:30[Asia/Kolkata]
Instanta point on the UTC time-line2026-01-14T20:10:00Z
Period / Durationdate-based / time-based amountP2Y8M / PT16H35M

3. Practical Examples

Example 1: Dates, periods and formatting

DateTimeBasics.java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Locale;

public class DateTimeBasics {
    public static void main(String[] args) {
        LocalDate joined = LocalDate.of(2024, 2, 29);          // a leap day
        LocalDate today = LocalDate.of(2026, 11, 27);

        // Immutable: plus/minus return new objects
        System.out.println("One year later : " + joined.plusYears(1));   // adjusted to Feb 28
        System.out.println("Day of week    : " + today.getDayOfWeek());

        Period served = Period.between(joined, today);
        System.out.println("Service period : " + served.getYears() + " years, "
                + served.getMonths() + " months, " + served.getDays() + " days");
        System.out.println("Days between   : " + ChronoUnit.DAYS.between(joined, today));

        LocalDate nextMonday = today.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        System.out.println("Next Monday    : " + nextMonday);

        LocalDateTime meeting = LocalDateTime.of(today, LocalTime.of(15, 30));
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd MMM yyyy, hh:mm a", Locale.US);
        System.out.println("Formatted      : " + meeting.format(fmt));
        System.out.println("Parsed         : " + LocalDate.parse("2026-12-31").isAfter(today));
    }
}
Output
One year later : 2025-02-28
Day of week    : FRIDAY
Service period : 2 years, 8 months, 29 days
Days between   : 1002
Next Monday    : 2026-11-30
Formatted      : 27 Nov 2026, 03:30 PM
Parsed         : true

Adding one year to 29 February 2024 gives 28 February 2025, because the API adjusts invalid dates to the last valid day. TemporalAdjusters answers questions like "next Monday" without manual arithmetic.

Example 2: Time zones and durations

A flight departs Bengaluru and lands in New York. withZoneSameInstant converts the arrival time to New York time while keeping the same moment.

TimeZonesAndDurations.java
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class TimeZonesAndDurations {
    public static void main(String[] args) {
        // A flight leaves Bengaluru at 01:40 local time
        ZonedDateTime departure = ZonedDateTime.of(
                LocalDateTime.of(2026, 1, 15, 1, 40), ZoneId.of("Asia/Kolkata"));

        // Flight time 16 h 35 min; convert the arrival to New York time
        Duration flight = Duration.ofHours(16).plusMinutes(35);
        ZonedDateTime arrivalNy = departure.plus(flight).withZoneSameInstant(ZoneId.of("America/New_York"));

        System.out.println("Departs (IST): " + departure);
        System.out.println("Arrives (NY) : " + arrivalNy);
        System.out.println("Flight time  : " + flight + " = " + flight.toMinutes() + " minutes");

        // Instant is a point on the UTC time-line, ideal for timestamps
        Instant stamp = departure.toInstant();
        System.out.println("As Instant   : " + stamp);
        System.out.println("Epoch second : " + stamp.getEpochSecond());
    }
}
Output
Departs (IST): 2026-01-15T01:40+05:30[Asia/Kolkata]
Arrives (NY) : 2026-01-15T07:45-05:00[America/New_York]
Flight time  : PT16H35M = 995 minutes
As Instant   : 2026-01-14T20:10:00Z
Epoch second : 1768421400

4. How the Types Relate

From local date-time to a global instant A LocalDateTime plus a ZoneId gives a ZonedDateTime, which maps to exactly one Instant on the UTC time-line. LocalDateTime 2026-01-15T01:40 + ZoneId Asia/Kolkata ZonedDateTime +05:30 offset Instant 2026-01-14T20:10Z
Figure 1: A local date-time becomes a real moment only when you add a time zone.

5. Important Points and Best Practices

  • All java.time types are immutable: methods like plusDays return new objects, so always use the return value.
  • Store timestamps as Instant (UTC) and convert to a zone only for display.
  • Use Period for years, months and days and Duration for hours, minutes and seconds.
  • Months are numbered 1 to 12, and DateTimeFormatter is thread-safe, unlike SimpleDateFormat.
  • For testable code, pass a Clock instead of calling now() directly.

6. Summary

  • java.time was added in Java 8 by JEP 150.
  • Use LocalDate, LocalDateTime, ZonedDateTime and Instant for different needs.
  • All types are immutable and thread-safe.
  • Period and Duration measure amounts of time.
  • Convert between zones with withZoneSameInstant.

Related Tutorials

7. References