Skip to main content

Lesson 6 of 10

Java Date & Time API (java.time)

The java.time package (Java 8, replacing the broken Date and Calendar classes) provides immutable, thread-safe date/time classes. LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Instant, and Duration are the core types. Every Java developer must know this API.

15 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Core Date-Time Classes

  • LocalDate - date only (year, month, day). No time, no timezone. Use for birthdays, due dates.
  • LocalTime - time only (hour, minute, second, nanosecond). No date, no timezone.
  • LocalDateTime - date + time, no timezone. Use for scheduling, logging (local).
  • ZonedDateTime - date + time + timezone. Use when timezone matters (user-facing events).
  • Instant - machine time (epoch seconds + nanoseconds). Use for timestamps in backend systems.
  • Duration - amount of time (hours, minutes, seconds). Between two time points.
  • Period - calendar duration (years, months, days). Between two dates.

java.time in Practice

java.time in Practice
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class DateTimeDemo {
    public static void main(String[] args) {
        // ─── Dates ───────────────────────────────
        LocalDate today     = LocalDate.now();
        LocalDate birthday  = LocalDate.of(1990, 3, 15);
        LocalDate tomorrow  = today.plusDays(1);
        LocalDate nextMonth = today.plusMonths(1);
        LocalDate dueDate   = today.plus(2, ChronoUnit.WEEKS);

        System.out.println("Today: " + today);         // 2024-03-20
        System.out.println("Due:   " + dueDate);
        System.out.println("Is before? " + birthday.isBefore(today)); // true

        long age = ChronoUnit.YEARS.between(birthday, today);
        System.out.println("Age: " + age + " years");

        // Period (calendar-aware difference)
        Period period = Period.between(birthday, today);
        System.out.printf("Age: %d years, %d months, %d days%n",
                           period.getYears(), period.getMonths(), period.getDays());

        // ─── DateTime & Formatting ───────────────
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);  // 2024-03-20T14:30:00.123456

        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
        System.out.println(now.format(fmt));  // 20/03/2024 14:30

        // Parse from string
        LocalDate parsed = LocalDate.parse("2024-12-25"); // ISO-8601 default
        LocalDate parsed2 = LocalDate.parse("25/12/2024",
                            DateTimeFormatter.ofPattern("dd/MM/yyyy"));

        // ─── Timestamps & Zones ──────────────────
        Instant timestamp = Instant.now();     // UTC epoch-based
        System.out.println(timestamp);         // 2024-03-20T09:00:00.000Z

        ZonedDateTime ist = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
        ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
        System.out.println("IST: " + ist.format(DateTimeFormatter.ofPattern("HH:mm z")));

        // Duration
        LocalDateTime start = LocalDateTime.of(2024, 1, 1, 9, 0);
        LocalDateTime end   = LocalDateTime.of(2024, 1, 3, 17, 30);
        Duration duration   = Duration.between(start, end);
        System.out.printf("Duration: %d hours, %d minutes%n",
                           duration.toHours(), duration.toMinutesPart());
    }
}

Quick Quiz

Tip

Tip

Practice Java Date Time API javatime in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Java Date & Time API (java.time)

Practice Task

Note

Practice Task - (1) Write a working example of Java Date Time API javatime from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Java Date Time API javatime is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Key Takeaways

  • LocalDate - date only (year, month, day). No time, no timezone. Use for birthdays, due dates.
  • LocalTime - time only (hour, minute, second, nanosecond). No date, no timezone.
  • LocalDateTime - date + time, no timezone. Use for scheduling, logging (local).

Topics in This Module

0/10 done