Stop 06 · Core Java APIsTopics · 7/8

Date & Time API (java.time)

Part of the Java Roadmap

In short

Due dates, EMI schedules, train departures across time zones, ages and subscriptions: almost every app handles time. The java.time package makes it clear and safe, unlike the confusing old Date and Calendar classes.

The java.time package (Java 8+) replaces the old Date and Calendar classes with clear, immutable types like LocalDate, LocalDateTime, ZonedDateTime, Duration and Period.

The jaadu, at a travel agent's desk, adjusts a row of wall clocks for different cities while holding a big paper desk calendar under one arm.

Think of it like this

The travel agent's wall

A travel agent in Kochi has a desk calendar for dates, a row of wall clocks for cities around the world, and a stopwatch for how long things take. Each tool does one job. java.time works the same way: LocalDate is the calendar, ZonedDateTime is the city clock, Duration and Period are the stopwatch.

Watch it happen

Pick the right java.time type

java.time
A moment
An amount of time
LocalDate
LocalDateTime
ZonedDateTime
Instant
Period
Duration

1/4First ask: is this a point in time, or a length of time?

The big ideas

What you need to know

Immutable and thread-safe

plusDays(30) returns a new date. The original never changes, so dates are safe to share.

Human-friendly types

LocalDate.of(2024, 7, 15) means 15 July. Months start at 1, unlike the old Calendar where July was 6.

Time zones are explicit

Use ZoneId.of("Asia/Kolkata"). Converting with withZoneSameInstant keeps the same moment and changes the clock reading.

Format and parse

DateTimeFormatter.ofPattern("dd MMM yyyy") formats dates for display and parses user input back.

See it in code

Walk through the code

java
1import java.time.Duration;2import java.time.LocalDate;3import java.time.LocalDateTime;4import java.time.LocalTime;5import java.time.Period;6import java.time.ZoneId;7import java.time.ZonedDateTime;8import java.time.format.DateTimeFormatter;9 10public class TimeDemo {11    public static void main(String[] args) {12        LocalDate joined = LocalDate.of(2024, 7, 15);13        LocalDate today = LocalDate.now();14        Period served = Period.between(joined, today);15        System.out.println("Worked " + served.getYears() + " years, " + served.getMonths() + " months");16 17        LocalDate due = joined.plusDays(30);   // joined is unchanged18        System.out.println(due);               // 2024-08-1419 20        LocalDateTime departs = LocalDateTime.of(2026, 3, 10, 6, 15);21        ZonedDateTime inDelhi = departs.atZone(ZoneId.of("Asia/Kolkata"));22        ZonedDateTime inDubai = inDelhi.withZoneSameInstant(ZoneId.of("Asia/Dubai"));23        System.out.println(inDubai.toLocalTime()); // 04:4524 25        Duration shift = Duration.between(LocalTime.of(9, 30), LocalTime.of(18, 0));26        System.out.println(shift.toHours() + "h " + shift.toMinutesPart() + "m"); // 8h 30m27 28        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd MMM yyyy");29        System.out.println(due.format(fmt));   // 14 Aug 202430    }31}

1/4Period.between counts whole years, months and days between two dates.

Dates, time zones, durations and formatting with java.time (Java 8+, toMinutesPart needs Java 9+).

Save yourself the pain

Common mistakes

Oops

Writing due.plusDays(7); and expecting due to move.

Do this instead

java.time objects are immutable. Write due = due.plusDays(7);.

Oops

Using java.util.Date and SimpleDateFormat in new code.

Do this instead

Use LocalDate, ZonedDateTime and DateTimeFormatter. The old classes are mutable and not thread-safe.

Oops

Storing server timestamps as LocalDateTime.

Do this instead

LocalDateTime has no zone, so the same value means different moments in Mumbai and London. Store an Instant or ZonedDateTime.

Your mission

Library due-date checker

~20 min

Quick check

Which type should you use for a person's date of birth?

Go deeper

  1. 1Calculate someone's age from their birth date with Period.
  2. 2Format a date as dd-MM-yyyy with DateTimeFormatter.
  3. 3Convert a time between Asia/Kolkata and another time zone.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge