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.
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.
Syntax
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.
Reference
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:
Type
What it holds
Use it for
LocalDate
Year, month, day
Birthdays, holidays, due dates (get one with toLocalDate())
LocalTime
Hour, minute, second, nano
Opening hours, alarm times (get one with toLocalTime())
LocalDateTime
Date and time, no zone
Appointments, schedules, and timestamps in one known place
ZonedDateTime / OffsetDateTime
Date, time, and a zone or offset
A real moment for people in different time zones (attach with atZone() or atOffset())
Old 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.
Concept
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.
Start
Call
Result
Rule
2026-01-31T09:00
plusMonths(1)
2026-02-28T09:00
Day is clamped to the last valid day of the shorter month
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().
Code
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.
date-time: 2026-11-27T17:30:45
year: 2026
month: NOVEMBER (11)
day: 27, FRIDAY
day of year: 331
time: 17:30:45
How It Works
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.
Number or enum.getMonthValue() returns 11 as an int, while getMonth() and getDayOfWeek() return the enums NOVEMBER and FRIDAY.
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.
The original never changes.start still prints 2026-01-31T09:00 after five calls, because each method returned a brand-new LocalDateTime.
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.
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.
isBefore: true
isAfter: false
hours between: 39
days between: 1
formatted: 24 Dec 2026, 18:00
date part: 2026-12-24, time part: 18:00
parsed equals end: true
with offset: 2026-12-26T09:30+05:30
How It Works
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.
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.
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.
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.