Java LocalDateTime Class

Beginner
10 min read
Updated: Nov 2026
3 examples
java.time.LocalDateTime

Definition and Usage

In Java, java.time.LocalDateTime is an immutable class that holds a date and a time of day together, such as 2026-12-25T09:30, with no time zone. It arrived in Java 8 with the java.time API and stores the year, month, day, hour, minute, second, and nanosecond. Think of it as what a wall calendar and a wall clock show at the same moment, without saying where in the world.

You never call a constructor. Create one with now(), of(), or parse(), read the parts with getYear(), getMonth(), and getHour(), move it with plusDays() or minusHours(), and compare it with isBefore() and isAfter(). The LocalDateTime Methods index lists all 62 guides.

Immutable, thread-safe, and clock dependent.

A LocalDateTime never changes after creation, so it is safe to share between threads. now() reads the system clock in the default time zone, so its result differs every run and from machine to machine; the examples on this page use fixed values from of() so the output is always the same.

Java Syntax

Import LocalDateTime and create it from the clock, from numbers, or from text:

Java
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

LocalDateTime now = LocalDateTime.now();                          // current date and time
LocalDateTime meeting = LocalDateTime.of(2026, 12, 25, 9, 30);    // 2026-12-25T09:30
LocalDateTime same = LocalDateTime.of(2026, Month.DECEMBER, 25, 9, 30);
LocalDateTime parsed = LocalDateTime.parse("2026-12-25T09:30:00"); // ISO-8601 text

LocalDateTime later = meeting.plusHours(2);                       // 2026-12-25T11:30
String text = meeting.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")); // 25/12/2026 09:30

Invalid values such as month 13 or February 30 throw DateTimeException, and text that does not match the format throws DateTimeParseException. When a time is exactly on the minute, toString() leaves out the seconds, so LocalDateTime.of(2026, 12, 25, 9, 30) prints as 2026-12-25T09:30.

LocalDateTime vs Other Date and Time Types

The java.time package has a type for each need. Pick the one that holds exactly what you have:

TypeWhat it holdsUse it for
LocalDateYear, month, dayBirthdays, holidays, due dates (get one with toLocalDate())
LocalTimeHour, minute, second, nanoOpening hours, alarm times (get one with toLocalTime())
LocalDateTimeDate and time, no zoneAppointments, schedules, and timestamps in one known place
ZonedDateTime / OffsetDateTimeDate, time, and a zone or offsetA real moment for people in different time zones (attach with atZone() or atOffset())
InstantA point on the UTC timelineMachine time, logs, and storing a moment (convert with ofInstant() or ofEpochSecond())
java.util.Date, CalendarLegacy, mutable moment in timeOld code only; convert and use java.time for new code

The key limit: without a zone, a LocalDateTime alone cannot say which instant it means. 2026-12-25T09:30 is a different moment in Chennai than in London.

Immutability and End-of-Month Rules

Every plus(), minus(), and with() method (and its specific versions like plusMonths(), minusYears(), and withHour()) returns a new object. Calling date.plusDays(1); and throwing the result away does nothing, so always assign: date = date.plusDays(1);. Methods can be chained, because each returns a LocalDateTime.

StartCallResultRule
2026-01-31T09:00plusMonths(1)2026-02-28T09:00Day is clamped to the last valid day of the shorter month
2028-02-29T09:00plusYears(1)2029-02-28T09:00Leap day moves to February 28 in a non-leap year
2026-01-31T09:00plusDays(10)2026-02-10T09:00Days roll over month ends normally
2026-03-31T09:00withMonth(2)2026-02-28T09:00withMonth() also clamps the day

Text, Formats, and Time Zones

toString() and parse() without a formatter use the ISO-8601 form 2026-12-25T09:30:15, with a capital T between the date and the time. For any other layout, pass a DateTimeFormatter to format() or parse(text, formatter); common pattern letters are yyyy (year), MM (month number), MMM (month name), dd (day), HH (24-hour), hh with a (12-hour with AM/PM), mm (minutes), and ss (seconds). Pass a Locale when month or day names must be in a specific language.

To measure the gap between two values, call until() with a unit such as ChronoUnit.HOURS, which counts complete units. To turn the value into a real moment, attach an offset or zone with atOffset() or atZone(). For code that handles any field generically, see get(), getLong(), isSupported(), range(), and query().

Java Examples

Example 1 — Create a Launch Time and Read Its Parts

Build a LocalDateTime from numbers, print it, and read the year, month, day, weekday, day of the year, and time parts.

Java
import java.time.LocalDateTime;

public class LocalDateTimeHubParts {
    public static void main(String[] args) {
        LocalDateTime launch = LocalDateTime.of(2026, 11, 27, 17, 30, 45);

        System.out.println("date-time: " + launch);
        System.out.println("year: " + launch.getYear());
        System.out.println("month: " + launch.getMonth() + " (" + launch.getMonthValue() + ")");
        System.out.println("day: " + launch.getDayOfMonth() + ", " + launch.getDayOfWeek());
        System.out.println("day of year: " + launch.getDayOfYear());
        System.out.println("time: " + launch.getHour() + ":" + launch.getMinute() + ":" + launch.getSecond());
    }
}

How It Works

  1. Fixed values, fixed output. of(2026, 11, 27, 17, 30, 45) builds the value without reading the clock, so every run prints the same text in ISO-8601 form.
  2. Number or enum. getMonthValue() returns 11 as an int, while getMonth() and getDayOfWeek() return the enums NOVEMBER and FRIDAY.
  3. Counting the days. January through October add up to 304 days, and 304 + 27 gives day 331 of the year 2026.

Example 2 — Add, Subtract, and Replace Parts Without Changing the Original

Start from January 31 and watch how plusMonths, plusDays, minusYears, withHour, and truncatedTo each return a new value.

Java
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class LocalDateTimeHubChange {
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2026, 1, 31, 9, 0);

        LocalDateTime nextMonth = start.plusMonths(1);
        LocalDateTime later = start.plusDays(10).plusHours(5);
        LocalDateTime lastYear = start.minusYears(1);
        LocalDateTime noon = start.withHour(12);
        LocalDateTime midnight = later.truncatedTo(ChronoUnit.DAYS);

        System.out.println("start:      " + start);
        System.out.println("plusMonths: " + nextMonth);
        System.out.println("days+hours: " + later);
        System.out.println("minusYears: " + lastYear);
        System.out.println("withHour:   " + noon);
        System.out.println("truncated:  " + midnight);
    }
}

How It Works

  1. The original never changes. start still prints 2026-01-31T09:00 after five calls, because each method returned a brand-new LocalDateTime.
  2. Month ends are clamped. February has no day 31, so plusMonths(1) settles on the last valid day, February 28. Adding 10 days instead rolls over normally to February 10, and 5 more hours move the clock from 09:00 to 14:00.
  3. Replace or cut. withHour(12) swaps only the hour, and truncatedTo(ChronoUnit.DAYS) sets every smaller part to zero, which produces midnight of February 10.

Example 3 — Compare, Measure, Format, Parse, and Add an Offset

Compare two event times, count the complete hours and days between them, print one in a custom pattern, parse text back, and attach a +05:30 offset.

Java
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;

public class LocalDateTimeHubCompareFormat {
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2026, 12, 24, 18, 0);
        LocalDateTime end = LocalDateTime.of(2026, 12, 26, 9, 30);

        System.out.println("isBefore: " + start.isBefore(end));
        System.out.println("isAfter: " + start.isAfter(end));
        System.out.println("hours between: " + start.until(end, ChronoUnit.HOURS));
        System.out.println("days between: " + start.until(end, ChronoUnit.DAYS));

        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("dd MMM yyyy, HH:mm", Locale.ENGLISH);
        System.out.println("formatted: " + start.format(pattern));
        System.out.println("date part: " + start.toLocalDate() + ", time part: " + start.toLocalTime());

        DateTimeFormatter input = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        LocalDateTime parsed = LocalDateTime.parse("2026-12-26 09:30", input);
        System.out.println("parsed equals end: " + parsed.equals(end));

        System.out.println("with offset: " + end.atOffset(ZoneOffset.ofHoursMinutes(5, 30)));
    }
}

How It Works

  1. Order and distance. start is earlier than end, so isBefore is true. The gap is 39 hours 30 minutes; until counts only complete units, so it reports 39 hours and 1 day.
  2. Format out, parse in. format(pattern) builds the text 24 Dec 2026, 18:00 (the Locale fixes the month name), and parse(text, input) reads a different pattern back into an equal LocalDateTime.
  3. Split and attach. toLocalDate() and toLocalTime() separate the two halves, and atOffset(...) returns an OffsetDateTime, which now identifies a real moment because the offset +05:30 is part of it.

Next: Java LocalDateTime Methods by Group

Full list with short descriptions: Java LocalDateTime Methods index.

Frequently Asked Questions

java.time.LocalDateTime is an immutable class, added in Java 8, that holds a date and a time of day such as 2026-12-25T09:30 without any time zone or offset. It is stored as year, month, day, hour, minute, second, and nanosecond, and it is thread-safe.
LocalDateTime has no zone, so on its own it does not identify one exact moment on the timeline; it is the wall-clock reading you would see on a calendar and clock. ZonedDateTime and OffsetDateTime add a zone or offset to pin a real moment, and Instant is a point on the UTC timeline. Use atZone() or atOffset() to attach a zone.
No. Every plus, minus, and with method returns a new LocalDateTime and leaves the original unchanged, so you must assign the result: date = date.plusDays(1). Immutability also makes LocalDateTime safe to share between threads.
When the target month is shorter than the starting day, LocalDateTime clamps to the last valid day of that month instead of throwing an exception. Adding one month to 2026-01-31 gives 2026-02-28, and a leap year would give February 29. The same rule applies to plusYears() from February 29.
Use format(DateTimeFormatter) to turn it into text and LocalDateTime.parse(text, formatter) to read text back. Without a formatter, toString() and parse(text) use the ISO-8601 form such as 2026-12-25T09:30. DateTimeFormatter.ofPattern lets you choose patterns like dd/MM/yyyy HH:mm.

Browse every LocalDateTime method

Open the methods index for categorized guides with runnable examples and FAQs.

LocalDateTime Methods Index →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful