Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

LocalDateTime

A date and time without a time zone — for example, a meeting in a calendar.

Available since: Java 8

Syntax

public final class LocalDateTime implements Temporal, ChronoLocalDateTime<LocalDate>

Examples

Creating, changing and reading parts:

void main() {
    LocalDateTime meeting = LocalDateTime.of(2025, 3, 14, 9, 30);
    IO.println(meeting);
    IO.println(meeting.plusHours(2).plusMinutes(15));
    IO.println(meeting.toLocalDate() + " / " + meeting.toLocalTime());
    IO.println(meeting.getHour());
}

Output:

2025-03-14T09:30
2025-03-14T11:45
2025-03-14 / 09:30
9

Comparing, and the start of a day:

void main() {
    LocalDateTime a = LocalDateTime.of(2025, 3, 14, 18, 0);
    LocalDateTime b = LocalDate.of(2025, 3, 15).atStartOfDay();
    IO.println(b);
    IO.println(a.isBefore(b));
}

Output:

2025-03-15T00:00
true

Notes

LocalDateTime has no time zone, so it does not identify an exact moment: 09:30 in Berlin and in Tokyo are different moments. For events compared across countries or stored on a server use Instant or ZonedDateTime. The current date and time is LocalDateTime.now().

See also

  • LocalDate.now() — Returns today's date from the computer's clock and time zone.
  • LocalTime — A time of day without a date — for example, a shop's opening time.
  • ZonedDateTime — A date and time in a specific time zone — with daylight saving time taken into account.
  • DateTimeFormatter.ofPattern() — Creates a formatter for printing and parsing dates with a pattern such as dd.MM.yyyy.
  • Duration — An amount of time in seconds and nanoseconds — hours, minutes, seconds.

Related recipes

In the tutorial

  • Dates and times — The java.time package — LocalDate, LocalTime, LocalDateTime, date arithmetic, Period, Duration, ChronoUnit, time zones and Instant.

Thanks for reading!

Donate to the site — we work hard for you.