Skip to content
Tutorials Center
My learning

Topics

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.

Java
LocalDateTime meeting = LocalDateTime.of(2025, 7, 14, 9, 30);
ZonedDateTime inParis = meeting.atZone(ZoneId.of("Europe/Paris"));

Try it

Java Creating, reading and changing

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.

Java Attaching a zone to get a real moment

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

Java
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:30 happened in many places at different times. Convert with a zone before comparing with real instants.
  • Daylight saving. atZone resolves gaps by moving the time forward by the gap length, and overlaps by choosing the earlier offset. Use ZonedDateTime.ofStrict or withEarlierOffsetAtOverlap / withLaterOffsetAtOverlap to control this.
  • Arithmetic is local. plusHours(24) always lands on the same local time the next day, ignoring any clock change; on a ZonedDateTime, plus hours counts real elapsed time.
  • Persistence. For a database, prefer TIMESTAMP WITH TIME ZONE mapped to Instant or OffsetDateTime. A LocalDateTime fits a plain TIMESTAMP meaning "wall clock".
  • Parsing needs the T. LocalDateTime.parse("2025-07-14 09:30") fails with the default formatter; use DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").
  • toString() drops zero seconds, so 09:30 round-trips; do not rely on a fixed length.
  • Precision: up to nanoseconds.