CKTIS / Java
Constructing LocalDateTime and finding the Monday of an ISO week
Build local dates and times from fixed inputs, find the Monday of the same ISO week, and choose the overload that returns the type you need.
Version: Java SE 21
Syntax
Java SE 21 — java.time
Static factories:
LocalDate LocalDate.of(int year, int month, int dayOfMonth)
LocalDate LocalDate.of(int year, Month month, int dayOfMonth)
LocalDateTime LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute)
LocalDateTime LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second)
LocalDateTime LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
The same three component overloads accept Month instead of int for month.
LocalDateTime LocalDateTime.of(LocalDate date, LocalTime time)
Instance methods on a LocalDate named date:
LocalDate date.with(TemporalAdjuster adjuster)
LocalDate date.with(TemporalField field, long newValue)
LocalDateTime date.atTime(int hour, int minute)
LocalDateTime date.atTime(int hour, int minute, int second)
LocalDateTime date.atTime(int hour, int minute, int second, int nanoOfSecond)
LocalDateTime date.atTime(LocalTime time)
OffsetDateTime date.atTime(OffsetTime time)
Common composition: date.with(DayOfWeek.MONDAY).atTime(9, 30)In java.time, the static factory LocalDate.of builds an ISO date from a year, month, and day of month. Pass the month as a number from 1 to 12 or a Month value such as Month.MARCH. The static factory LocalDateTime.of takes the same date components plus hour and minute, with overloads that also accept seconds and then nanoseconds. Omitted seconds and nanoseconds default to zero. Another overload combines an existing LocalDate and LocalTime.
The instance method LocalDate.with leaves the original date unchanged: use the returned value. In date.with(DayOfWeek.MONDAY), the enum value acts as a TemporalAdjuster and selects the Monday of the same ISO week (Monday through Sunday), regardless of locale. If the date is already Monday, its value stays the same; otherwise the result is earlier and may fall in a different month or year. The with(TemporalField, long) overload sets a field to a numeric value. For ChronoField.DAY_OF_WEEK, 1 also selects the Monday of the same ISO week.
The instance method LocalDate.atTime combines the date with a LocalTime or with time components: hour and minute, optionally followed by seconds and then nanoseconds. Omitted components default to zero. With a LocalTime, date.atTime(time) yields the same value as LocalDateTime.of(date, time). The OffsetTime overload instead returns an OffsetDateTime and preserves the UTC offset.
The of factories throw DateTimeException for an invalid date, and overloads taking numeric time components throw it for out-of-range components such as hour 24. Both LocalDate and LocalDateTime are immutable. A LocalDateTime has neither a time zone nor a UTC offset, so it does not identify an instant by itself.
Examples
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2024, Month.MARCH, 1);
LocalDate monday = date.with(DayOfWeek.MONDAY);
LocalDateTime adjusted = monday.atTime(9, 30);
LocalDateTime direct = LocalDateTime.of(2024, 2, 26, 9, 30);
LocalDateTime combined = LocalDateTime.of(monday, LocalTime.of(9, 30));
System.out.println(date);
System.out.println(adjusted);
System.out.println(adjusted.equals(direct) && adjusted.equals(combined));
}
}Result
2024-03-01 2024-02-26T09:30 true
The input is Friday `2024-03-01`. Its Monday, `2024-02-26`, falls in the previous month. After adding `09:30`, all three approaches yield equal `LocalDateTime` values, as shown by `true`.
Related lessons
- Java · Lesson 60 · Advanced · Version: 2