Java · Core APIs

Date and Time API

Dates and times are tricky: time zones, leap years, formatting. The java.time API handles them correctly.

Intermediate 5 min Lesson 24 of 31Examples target Java 21 LTS (they also run on Java 25).
Before you start:File I/O
0/31 in Java
On this page · 5 min · Intermediate

In simple words#

LocalDate is a date on a wall calendar (no time zone). ZonedDateTime is a meeting invite that says “10:00 IST”. Instant is the exact moment on a world clock, the same everywhere.

LocalDate
Date without time or zone.
ZonedDateTime
Date-time in a specific time zone.
Duration / Period
Time-based (hours) / date-based (days, months) amounts.

Learning objectives#

  • Create and manipulate dates
  • Work with time zones
  • Format and parse dates
  • Calculate durations and periods

Explanation#

Use LocalDate for birthdays and due dates, LocalDateTime for local timestamps, ZonedDateTime for zone-aware times, Instant for machine timestamps (store in UTC).

All java.time objects are immutable: plusDays returns a new object.

DateTimeFormatter.ofPattern("dd MMM yyyy") formats and parses.

Avoid old Date and Calendar classes in new code.

Code example: Due dates and time zones#

JavaDue dates and time zones Illustrative
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;


LocalDate ordered = LocalDate.of(2026, 10, 28);
LocalDate delivery = ordered.plusDays(3);
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd MMM yyyy");
System.out.println("Delivery: " + delivery.format(f) + " (" + delivery.getDayOfWeek() + ")");


ZonedDateTime meetingIst = ZonedDateTime.of(2026, 11, 2, 10, 0, 0, 0, ZoneId.of("Asia/Kolkata"));
ZonedDateTime meetingLondon = meetingIst.withZoneSameInstant(ZoneId.of("Europe/London"));
System.out.println("London time: " + meetingLondon.toLocalTime());


long days = ChronoUnit.DAYS.between(LocalDate.of(2026, 1, 1), LocalDate.of(2026, 10, 30));
Period age = Period.between(LocalDate.of(2000, 5, 15), LocalDate.of(2026, 10, 30));
System.out.println(days + " days since New Year; age " + age.getYears() + " years");
System.out.println("Stored as UTC: " + meetingIst.toInstant());
Expected output
Delivery: 31 Oct 2026 (SATURDAY)
London time: 04:30
302 days since New Year; age 26 years
Stored as UTC: 2026-11-02T04:30:00Z

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. plusDays returns a new LocalDate.
  2. A formatter prints a friendly date.
  3. withZoneSameInstant converts IST to London time.
  4. ChronoUnit and Period calculate differences; Instant shows UTC.

Real-world use case#

An e-commerce site stores order times as UTC Instants and shows them in each customer’s local time zone.

Common mistakes#

Using java.util.Date in new code

It is mutable and confusing. Use java.time.

Storing local times without zone

Store UTC (Instant) and convert for display.

Best practices#

  • Store Instants in UTC
  • Convert at display time
  • Immutable java.time types
  • Use ZoneId names like Asia/Kolkata

Remember this#

LocalDate = calendar date. Instant = exact moment (UTC).

java.time objects are immutable.

Store UTC, display local.

Practice exercise#

Your task: Find the date 45 days after today.

Show solution
Java Illustrative
LocalDate later = LocalDate.now().plusDays(45);

Mini quiz#

Date and Time API quizMultiple choice Question 1 of 1

Which type represents an exact moment in UTC?

Score so far: 0/0

Want more? Practise all Java MCQs · Java interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.