About Us Contact Us Write for Us Advertise
Home > Java > Java Date and Time API: Working with LocalDate and LocalDateTime
Java

Java Date and Time API: Working with LocalDate and LocalDateTime

Learn the modern Java Date & Time API (java.time) — LocalDate, LocalTime, LocalDateTime, date maths, comparing, formatting with DateTimeFormatter, and measuring gaps.

Shiv Pandey
Shiv Pandey
Sep 27, 2026 | 39 views
Java Date and Time API: Working with LocalDate and LocalDateTime

Almost every real application deals with dates and times — a blog post's publish date, a user's signup timestamp, "3 days ago," scheduling something for next week. Java's modern Date & Time API (from java.time, introduced in Java 8) makes this clean and pleasant. This lesson shows you the handful of classes you'll actually use — and steers you away from the messy old ones.

First, a heads-up: you'll see old tutorials using java.util.Date and Calendar. Avoid them — they're clunky, error-prone, and (surprisingly) mutable. Modern Java replaced them with the far better java.time package. Everything below uses the modern, recommended classes.

The three classes you'll use most

Class Represents Example
LocalDate A date (no time) 2026-09-17
LocalTime A time (no date) 14:30:00
LocalDateTime Both together 2026-09-17T14:30

Getting the current date and time

import java.time.*;

LocalDate today = LocalDate.now();          // 2026-09-17
LocalTime now   = LocalTime.now();          // 14:30:52.123
LocalDateTime dt = LocalDateTime.now();     // 2026-09-17T14:30:52

Creating a specific date

LocalDate birthday = LocalDate.of(2000, 5, 15);   // year, month, day
System.out.println(birthday.getDayOfWeek());       // MONDAY

Doing date maths (and immutability again)

Adding or subtracting time is beautifully readable. Note: these classes are immutable, so each operation returns a new date — just like Strings:

LocalDate today = LocalDate.now();

LocalDate nextWeek = today.plusDays(7);        // 7 days from now
LocalDate lastMonth = today.minusMonths(1);
LocalDate deadline = today.plusWeeks(2).plusDays(3);   // chainable!

// today is unchanged — a new object was returned each time

Comparing dates

LocalDate d1 = LocalDate.of(2026, 1, 1);
LocalDate d2 = LocalDate.of(2026, 12, 31);

System.out.println(d1.isBefore(d2));   // true
System.out.println(d1.isAfter(d2));    // false

Formatting dates for display

To show a date in a friendly format, use a DateTimeFormatter:

import java.time.format.DateTimeFormatter;

LocalDate date = LocalDate.now();
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd MMM yyyy");

System.out.println(date.format(fmt));   // 17 Sep 2026

The reverse — turning text into a date — uses LocalDate.parse(text, fmt). Together they cover reading dates in and printing them out nicely.

Measuring the gap between dates

import java.time.temporal.ChronoUnit;

LocalDate start = LocalDate.of(2026, 1, 1);
LocalDate end   = LocalDate.of(2026, 9, 17);

long days = ChronoUnit.DAYS.between(start, end);   // 259

The java.time API is one of those parts of modern Java that's just genuinely nice to use — readable, safe, and immutable, which sidesteps a whole family of old date bugs. On this website, every blog post carries created/published/scheduled timestamps, and the scheduling feature that decides when a post goes live is all built on exactly these classes. Reach for LocalDate, LocalTime, and LocalDateTime — and leave the old Date/Calendar classes in the past where they belong.

Key takeaways

  • Use the modern java.time API — LocalDate, LocalTime, LocalDateTime — not old Date/Calendar.
  • Get current values with .now(); build specific ones with .of(...).
  • Do date maths with plusDays/minusMonths (immutable — returns new objects); compare with isBefore/isAfter.
  • Format for display with DateTimeFormatter; measure gaps with ChronoUnit.DAYS.between.

← Previous: Lesson 31 — File I/O in Java
Next: Lesson 33 — JVM, Memory & Garbage Collection →

Related Articles

Time and Space Complexity in Java: Big O Notation Explained
Java

Time and Space Complexity in Java: Big O Notation Explained

The Streams API in Java: Process Collections the Modern Way
Java

The Streams API in Java: Process Collections the Modern Way

== vs .equals() in Java: The Difference Explained (Beginner Guide)
Java

== vs .equals() in Java: The Difference Explained (Beginner Guide)

Java vs Python: Which Should You Learn? (Beginner Guide)
Java

Java vs Python: Which Should You Learn? (Beginner Guide)