Date and Time API
Dates and times are tricky: time zones, leap years, formatting. The java.time API handles them correctly.
On this page · 5 min · Intermediate
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 30, 2026
Related tutorials
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#
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());
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#
- plusDays returns a new LocalDate.
- A formatter prints a friendly date.
- withZoneSameInstant converts IST to London time.
- 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#
It is mutable and confusing. Use java.time.
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
LocalDate later = LocalDate.now().plusDays(45);
Mini quiz#
Which type represents an exact moment in UTC?
Want more? Practise all Java MCQs · Java interview questions
My notes#
Saved in this browser.