Java LocalDateTime withMinute() Method

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

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.

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

Parameters

ParameterDescription
minute (int)The new minute, from 0 to 59.

Return Value

Return ValueDescription
LocalDateTimeA new LocalDateTime with the minute 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.
plusMinutes(n)Adds minutes to the current minute instead of setting one.
withSecond(s)Sets the second and keeps the minute.

Java Examples

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.

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

How It Works

  1. An untidy time. 10:17:42 has minutes and seconds.
  2. Set the minute to 30. The result is 10:30:42: the seconds are still there.
  3. 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.

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

How It Works

  1. The start. 47 minutes past ten.
  2. Minute 0. The time becomes 10:00, the top of the hour.
  3. 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.

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

How It Works

  1. 60 is too big. There are only 60 minutes in an hour, numbered 0 to 59.
  2. The exception is caught. withMinute(60) throws a DateTimeException, and the catch block prints its name.
  3. 59 is valid. The last line prints 10:59 on the same date.

Frequently Asked Questions

Call dt.withMinute(value). It returns a new LocalDateTime with that part replaced and everything else copied.
No. LocalDateTime is immutable, so withMinute() returns a new object and leaves the original as it was.
A minute below 0 or above 59 throws a DateTimeException. In particular, 60 is not allowed.
Use dt.withMinute(15). It returns a new LocalDateTime with the minute replaced.
Use dt.truncatedTo(ChronoUnit.HOURS), which clears the minutes, seconds, and fraction in one call.

Next: withMonth()

Return a copy with a different month.

withMonth() 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.

10 people found this page helpful