Java LocalDateTime withHour() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

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.

Java
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
CallWhat 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
// java.time.LocalDateTime
public LocalDateTime withHour(int hour)

Parameters

ParameterDescription
hour (int)The new hour, from 0 to 23.

Return Value

Return ValueDescription
LocalDateTimeA new LocalDateTime with the hour replaced. The original is not changed.

Compare with Similar Methods

Ways to change one part of a date-time:

MethodHow it differs
with(field, value)The general method: you choose the field as an argument.
plusHours(n)Adds hours to the current hour instead of setting one.
withMinute(m)Sets the minute and keeps the hour.

Java Examples

Example 1 — Set the Hour of the Day

Take 10:30 and set the hour to 18 and to 0.

Java
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));
    }
}

How It Works

  1. The start. The time is half past ten in the morning, on 15 March 2026.
  2. Hour 18. withHour(18) gives 18:30. The minutes stay at 30.
  3. 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.

Java
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);
    }
}

How It Works

  1. A precise time. 10:30:45.123 has minutes, seconds, and milliseconds.
  2. Set the hour to 7. The result starts with 07.
  3. 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.

Java
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));
    }
}

How It Works

  1. 24 is too big. The 24-hour clock goes from 0 to 23, so withHour(24) throws a DateTimeException.
  2. Negative hours fail too. withHour(-1) throws the same exception.
  3. 23 is the largest valid hour. The last line prints 23:30.

Frequently Asked Questions

Call dt.withHour(value). It returns a new LocalDateTime with that part replaced and everything else copied.
No. LocalDateTime is immutable, so withHour() returns a new object and leaves the original as it was.
An hour below 0 or above 23 throws a DateTimeException. In particular, 24 is not allowed.
Use dt.withHour(9). It returns a new LocalDateTime with the hour replaced and everything else copied.
No. Valid hours are 0 to 23. For the next midnight, use dt.plusDays(1).withHour(0).

Next: withMinute()

Return a copy with a different minute.

withMinute() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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