Home Java LocalDateTime Methods withMinute() Java LocalDateTime withMinute() Method Definition In Java, withMinute(minute) returns a new LocalDateTime with the minute set to the value you pass. The date, the hour, 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.withMinute(45);. The hour never changes, even when you set 0 or 59 minutes.
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 minute.
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime result = start.withMinute(45); // 2026-03-15T10:45
System.out.println(start); // 2026-03-15T10:30 Call What it does dt.withMinute(n)Returns a copy with the minute replaced by n. dt.with(ChronoField.MINUTE_OF_HOUR, n)The general form for any field. It does the same thing here. dt.plusMinutes(n)Moves by an amount instead of setting a value. time.withMinute(m)LocalTime has the same method, but it has no date.
The result is a new object . Everything except the minute is copied from the original.
Library declarations // java.time.LocalDateTime
public LocalDateTime withMinute(int minute) Parameters Parameter Description minute (int)The new minute, from 0 to 59.
Return Value Return Value Description LocalDateTimeA new LocalDateTime with the minute replaced. The original is not changed.
Important
Only the minute changes. The seconds and the fraction stay, so a time like 10:17:42 becomes 10:30:42 and not 10:30:00. Call withSecond(0) as well, or truncatedTo(ChronoUnit.MINUTES), if you want the seconds cleared.
Compare with Similar Methods Ways to change one part of a date-time:
Java Examples 1. Half-hour slot 2. Top of the hour 3. Invalid minute Example 1 — Move a Time to the Half Hour Take 10:17:42 and set the minute to 30. Then clear the seconds as well.
import java.time.LocalDateTime;
public class LdtWithMinuteDemo {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 17, 42);
LocalDateTime half = start.withMinute(30);
LocalDateTime clean = half.withSecond(0);
System.out.println("start: " + start);
System.out.println("minute 30: " + half);
System.out.println("seconds gone: " + clean);
}
} start: 2026-03-15T10:17:42
minute 30: 2026-03-15T10:30:42
seconds gone: 2026-03-15T10:30 How It Works An untidy time. 10:17:42 has minutes and seconds.Set the minute to 30. The result is 10:30:42: the seconds are still there.Clear the seconds too. withSecond(0) gives exactly 10:30, which prints without seconds.Example 2 — Reset to the Top of the Hour Take 10:47 and set the minute to 0 and to 59.
import java.time.LocalDateTime;
public class LdtWithMinuteHour {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 47);
System.out.println("start: " + start);
System.out.println("minute 0: " + start.withMinute(0));
System.out.println("minute 59: " + start.withMinute(59));
}
} start: 2026-03-15T10:47
minute 0: 2026-03-15T10:00
minute 59: 2026-03-15T10:59 How It Works The start. 47 minutes past ten.Minute 0. The time becomes 10:00, the top of the hour.Minute 59. The time becomes 10:59. The hour does not go up.Example 3 — Try a Minute That Does Not Exist Set the minute to 60 and to 59.
import java.time.DateTimeException;
import java.time.LocalDateTime;
public class LdtWithMinuteInvalid {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
try {
start.withMinute(60);
} catch (DateTimeException e) {
System.out.println("withMinute(60) -> DateTimeException");
}
System.out.println("withMinute(59) -> " + start.withMinute(59));
}
} withMinute(60) -> DateTimeException
withMinute(59) -> 2026-03-15T10:59 How It Works 60 is too big. There are only 60 minutes in an hour, numbered 0 to 59.The exception is caught. withMinute(60) throws a DateTimeException, and the catch block prints its name.59 is valid. The last line prints 10:59 on the same date.Frequently Asked Questions How do I change only the minute of a LocalDateTime in Java? Call dt.withMinute(value). It returns a new LocalDateTime with that part replaced and everything else copied.
Does withMinute() change the original LocalDateTime? No. LocalDateTime is immutable, so withMinute() returns a new object and leaves the original as it was.
What happens if the value passed to withMinute() is not valid? A minute below 0 or above 59 throws a DateTimeException. In particular, 60 is not allowed.
How do I set the minutes of a LocalDateTime in Java? Use dt.withMinute(15). It returns a new LocalDateTime with the minute replaced.
How do I round a LocalDateTime down to the hour? Use dt.truncatedTo(ChronoUnit.HOURS), which clears the minutes, seconds, and fraction in one call.
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.
10 people found this page helpful
Helpful Share Copy link Suggestion