Java Reference ยท Date and Time
The LocalDateTime class
Reference for java.time.LocalDateTime: a date and time without a zone, creating and parsing values, arithmetic, comparison, and converting to zoned types.
java.time.LocalDateTime combines a LocalDate and a LocalTime: 2025-07-14T09:30:00. It has no time zone and no offset, so it does not identify a single moment on the timeline. It is the right type for things described the way people say them on a wall calendar: "the meeting is on July 14 at 9:30", "the store opens at 08:00 on weekdays".
Because it carries no zone, a LocalDateTime cannot be compared to a real moment until you attach a zone with atZone (giving a ZonedDateTime) or an offset with atOffset. Do not use it for timestamps of events that actually happened, such as log entries or created-at columns; use Instant.
LocalDateTime meeting = LocalDateTime.of(2025, 7, 14, 9, 30); ZonedDateTime inParis = meeting.atZone(ZoneId.of("Europe/Paris"));
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public final class LocalDateTime implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable
| Member | Description |
|---|---|
now(), now(ZoneId), now(Clock) |
Current date-time in the default zone, a zone or a clock. |
of(y, m, d, h, min) (and + s, + nano) |
From fields; month may be a Month. |
of(LocalDate, LocalTime) |
Combine a date and a time. |
parse(CharSequence) |
ISO text such as 2025-07-14T09:30. |
toLocalDate(), toLocalTime() |
Split into parts. |
getYear() ... getNano(), getDayOfWeek() |
Field accessors. |
plusDays/Hours/Minutes/..., minus..., plus(Duration/Period) |
Arithmetic. |
withHour(int), with(...), truncatedTo(unit) |
Replace or truncate fields. |
atZone(ZoneId), atOffset(ZoneOffset), toInstant(ZoneOffset) |
Attach a zone or offset. |
isBefore, isAfter, isEqual, compareTo, equals |
Comparison. |
Throws: DateTimeException for invalid fields, DateTimeParseException for bad text.
Usage notes
- Not a moment.
2025-07-14T09:30happened in many places at different times. Convert with a zone before comparing with real instants. - Daylight saving.
atZoneresolves gaps by moving the time forward by the gap length, and overlaps by choosing the earlier offset. UseZonedDateTime.ofStrictorwithEarlierOffsetAtOverlap/withLaterOffsetAtOverlapto control this. - Arithmetic is local.
plusHours(24)always lands on the same local time the next day, ignoring any clock change; on aZonedDateTime, plus hours counts real elapsed time. - Persistence. For a database, prefer
TIMESTAMP WITH TIME ZONEmapped toInstantorOffsetDateTime. ALocalDateTimefits a plainTIMESTAMPmeaning "wall clock". - Parsing needs the
T.LocalDateTime.parse("2025-07-14 09:30")fails with the default formatter; useDateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"). toString()drops zero seconds, so09:30round-trips; do not rely on a fixed length.- Precision: up to nanoseconds.
Related
LocalDate,LocalTime,ZonedDateTime,Instant,DateTimeFormatter- Tutorial: Dates and Times