Home Java LocalDateTime Methods withHour() Java LocalDateTime withHour() Method Definition In Java, withHour(hour) returns a new LocalDateTime with the hour set to the value you pass. The date, the minutes, the seconds, and the nanoseconds are copied from the original.
LocalDateTime is immutable , so the original object never changes. Always keep the result: dt = dt.withHour(18);. The hour uses a 24-hour clock, so 0 is midnight and 23 is eleven at night.
An instance method of java.time.LocalDateTime (since Java 8): call it on a LocalDateTime object. Add import java.time.LocalDateTime; at the top of your file.
Syntax Call it on a LocalDateTime and pass the new hour.
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime result = start.withHour(18); // 2026-03-15T18:30
System.out.println(start); // 2026-03-15T10:30 Call What it does dt.withHour(n)Returns a copy with the hour replaced by n. dt.with(ChronoField.HOUR_OF_DAY, n)The general form for any field. It does the same thing here. dt.plusHours(n)Moves by an amount instead of setting a value. time.withHour(h)LocalTime has the same method, but it has no date.
The result is a new object . Everything except the hour is copied from the original.
Library declarations // java.time.LocalDateTime
public LocalDateTime withHour(int hour) Parameters Parameter Description hour (int)The new hour, from 0 to 23.
Return Value Return Value Description LocalDateTimeA new LocalDateTime with the hour replaced. The original is not changed.
Important
Only the hour changes. The date never moves, even for 0 or 23, and the minutes, seconds, and fraction are kept. There is no 24: use the next day's midnight with plusDays(1) and withHour(0) if you need it.
Compare with Similar Methods Ways to change one part of a date-time:
Java Examples 1. Move to the evening 2. Only the hour changes 3. Invalid hour Example 1 — Set the Hour of the Day Take 10:30 and set the hour to 18 and to 0.
import java.time.LocalDateTime;
public class LdtWithHourDemo {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
System.out.println("start: " + start);
System.out.println("hour 18: " + start.withHour(18));
System.out.println("hour 0: " + start.withHour(0));
}
} start: 2026-03-15T10:30
hour 18: 2026-03-15T18:30
hour 0: 2026-03-15T00:30 How It Works The start. The time is half past ten in the morning, on 15 March 2026.Hour 18. withHour(18) gives 18:30. The minutes stay at 30.Hour 0. This gives 00:30, which is half past midnight on the same date.Example 2 — See What the Method Keeps Start with a time that has seconds and milliseconds, and change only the hour.
import java.time.LocalDateTime;
public class LdtWithHourKept {
public static void main(String[] args) {
LocalDateTime precise = LocalDateTime.of(2026, 3, 15, 10, 30, 45, 123_000_000);
LocalDateTime changed = precise.withHour(7);
System.out.println("before: " + precise);
System.out.println("after: " + changed);
}
} before: 2026-03-15T10:30:45.123
after: 2026-03-15T07:30:45.123 How It Works A precise time. 10:30:45.123 has minutes, seconds, and milliseconds.Set the hour to 7. The result starts with 07.Everything else is kept. The :30:45.123 part and the date are unchanged.Example 3 — Try Hours That Do Not Exist Set the hour to 24, to -1, and to 23.
import java.time.DateTimeException;
import java.time.LocalDateTime;
public class LdtWithHourInvalid {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
try {
start.withHour(24);
} catch (DateTimeException e) {
System.out.println("withHour(24) -> DateTimeException");
}
try {
start.withHour(-1);
} catch (DateTimeException e) {
System.out.println("withHour(-1) -> DateTimeException");
}
System.out.println("withHour(23) -> " + start.withHour(23));
}
} withHour(24) -> DateTimeException
withHour(-1) -> DateTimeException
withHour(23) -> 2026-03-15T23:30 How It Works 24 is too big. The 24-hour clock goes from 0 to 23, so withHour(24) throws a DateTimeException.Negative hours fail too. withHour(-1) throws the same exception.23 is the largest valid hour. The last line prints 23:30.Frequently Asked Questions How do I change only the hour of a LocalDateTime in Java? Call dt.withHour(value). It returns a new LocalDateTime with that part replaced and everything else copied.
Does withHour() change the original LocalDateTime? No. LocalDateTime is immutable, so withHour() returns a new object and leaves the original as it was.
What happens if the value passed to withHour() is not valid? An hour below 0 or above 23 throws a DateTimeException. In particular, 24 is not allowed.
How do I set the hour of a LocalDateTime in Java? Use dt.withHour(9). It returns a new LocalDateTime with the hour replaced and everything else copied.
Can withHour(24) be used for midnight? No. Valid hours are 0 to 23. For the next midnight, use dt.plusDays(1).withHour(0).
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion