0Pricing
Java Academy · Lesson

LocalDate, LocalTime, LocalDateTime

Represent dates and times.

LocalDate, LocalTime, LocalDateTime is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Modern Date API

Since Java 8 the java.time package replaced the old, error-prone Date and Calendar classes. Its core types are LocalDate, LocalTime, and LocalDateTime.

All of them are immutable and thread-safe.

LocalDate

LocalDate represents a date with no time component: a year, month, and day. LocalDate.now() gives today, but tests should use a fixed date for predictable output.

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2026, 5, 30);
        System.out.println(date);
    }
}

Reading Date Parts

You can pull out each component with getYear, getMonthValue, getDayOfMonth, and the handy getDayOfWeek.

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2026, 5, 30);
        System.out.println(date.getYear());
        System.out.println(date.getMonthValue());
        System.out.println(date.getDayOfWeek());
    }
}

LocalTime

LocalTime represents a time of day without a date: hours, minutes, seconds, and nanoseconds.

import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        LocalTime time = LocalTime.of(14, 30, 15);
        System.out.println(time);
        System.out.println(time.getHour());
    }
}

LocalDateTime

LocalDateTime combines a date and a time into one value. It still has no time zone attached.

import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 5, 30, 9, 45);
        System.out.println(dt);
    }
}

Combining Date and Time

You can merge a LocalDate with a LocalTime using atTime, producing a LocalDateTime.

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDate d = LocalDate.of(2026, 5, 30);
        LocalTime t = LocalTime.of(8, 0);
        LocalDateTime dt = d.atTime(t);
        System.out.println(dt);
    }
}

Immutability and with

These types are immutable. Methods like withYear return a new modified copy and leave the original unchanged.

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate original = LocalDate.of(2026, 5, 30);
        LocalDate changed = original.withYear(2030);
        System.out.println(original);
        System.out.println(changed);
    }
}

Comparing Dates

Use isBefore, isAfter, and isEqual to compare chronological order in a readable way.

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate a = LocalDate.of(2026, 1, 1);
        LocalDate b = LocalDate.of(2026, 12, 31);
        System.out.println(a.isBefore(b));
        System.out.println(a.isAfter(b));
    }
}

Useful Queries

Convenience methods answer common questions, like isLeapYear and lengthOfMonth.

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2024, 2, 1);
        System.out.println(date.isLeapYear());
        System.out.println(date.lengthOfMonth());
    }
}

Parsing From Text

Each type can parse an ISO-formatted string. LocalDate.parse reads a value like 2026-05-30.

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.parse("2026-05-30");
        System.out.println(date.getMonth());
    }
}

Choosing the Right Type

Use LocalDate for birthdays and calendar dates, LocalTime for opening hours, and LocalDateTime for an appointment that pairs a date with a time. None of them carry a time zone.

Quick Check

Test your grasp of the core date and time types.

Recap

You learned the core java.time types:

  • LocalDate for dates, LocalTime for times, LocalDateTime for both
  • All are immutable; with methods return new copies
  • Read parts with getYear, getDayOfWeek, and similar getters
  • Compare with isBefore and isAfter
  • Parse ISO strings with parse

Frequently asked questions

Is the “LocalDate, LocalTime, LocalDateTime” lesson free?

Yes — the full text of “LocalDate, LocalTime, LocalDateTime” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “LocalDate, LocalTime, LocalDateTime”?

Represent dates and times. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “LocalDate, LocalTime, LocalDateTime” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. LocalDate, LocalTime, LocalDateTime
  2. Instant and Duration
  3. Period and Date Arithmetic
  4. Formatting with DateTimeFormatter
← Back to Java Academy