Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

LocalDateTime — date and time without a time zone

LocalDateTime stores an ISO calendar date and time without a time zone or UTC offset. It supports creating values, adding hours and minutes, and formatting the result without changing the original value.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute)
LocalDateTime.plusHours(long hours)
LocalDateTime.plusMinutes(long minutes)
LocalDateTime.format(DateTimeFormatter formatter)

java.time.LocalDateTime represents a date and time without a time zone or UTC offset. Alone, it does not identify a unique instant on the timeline. Do not use it to calculate actual elapsed time across a daylight-saving transition; that requires zone context or Instant values.

LocalDateTime.of(year, month, dayOfMonth, hour, minute) creates a value with seconds and nanoseconds set to zero. Months are numbered 1–12. An invalid date, such as February 30, throws DateTimeException.

LocalDateTime.plusHours(long) and LocalDateTime.plusMinutes(long) return a result without changing the original, so retain the result. They also accept negative amounts. Crossing midnight updates the date; exceeding the supported range throws DateTimeException.

LocalDateTime.format(DateTimeFormatter) returns a String. In the pattern uuuu-MM-dd HH:mm, MM means month, mm means minutes, and HH means a 24-hour clock hour. The example uses fixed inputs and Locale.ROOT, demonstrating rollover into the next year and that start remains unchanged.

Examples

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2024, 12, 31, 23, 30);
        LocalDateTime end = start.plusHours(1).plusMinutes(45);
        DateTimeFormatter formatter =
                DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm", Locale.ROOT);

        System.out.println(start.format(formatter));
        System.out.println(end.format(formatter));
    }
}

Result

2024-12-31 23:30
2025-01-01 01:15

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources