Java LocalDateTime with() Method

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

Definition

In Java, with(...) returns a new LocalDateTime that is a changed copy of the original. There are two versions. One takes a TemporalAdjuster, a rule such as "go to the last day of the month". The other takes a ChronoField and a value, such as "set the hour to 9".

The class TemporalAdjusters has many ready-made rules, and a LocalDate or a LocalTime can also be used as an adjuster to replace the date or the time part.

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. It comes from Temporal, which LocalDateTime implements.

Syntax

Pass an adjuster, or a field and a value.

Java
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime a = dt.with(TemporalAdjusters.lastDayOfMonth());    // 2026-03-31T10:30
LocalDateTime b = dt.with(TemporalAdjusters.next(DayOfWeek.MONDAY)); // 2026-03-16T10:30
LocalDateTime c = dt.with(ChronoField.HOUR_OF_DAY, 9);            // 2026-03-15T09:30
CallWhat it does
dt.with(adjuster)Applies a rule, such as TemporalAdjusters.lastDayOfMonth().
dt.with(field, value)Sets one ChronoField to a value.
dt.with(LocalDate)Replaces the date part and keeps the time.
dt.with(LocalTime)Replaces the time part and keeps the date.

Like every method of LocalDateTime, with() returns a new object and leaves the original unchanged.

Library declarations
Java
// java.time.LocalDateTime
public LocalDateTime with(TemporalAdjuster adjuster)
public LocalDateTime with(TemporalField field, long newValue)

Parameters

ParameterDescription
adjuster (TemporalAdjuster)A rule such as TemporalAdjusters.firstDayOfMonth(), a LocalDate, or a LocalTime.
field (TemporalField)The field to set, such as ChronoField.HOUR_OF_DAY. Use with the next argument.
newValue (long)The new value of the field. It must be inside the range of the field.

Return Value

Return ValueDescription
LocalDateTimeA new LocalDateTime with the change applied.

Compare with Similar Methods

Ways to change a date-time:

MethodHow it differs
withHour(h)A shortcut for with(ChronoField.HOUR_OF_DAY, h).
plus(n, unit)Moves by an amount instead of setting a value.
truncatedTo(unit)Sets all the parts below a unit to zero.

Java Examples

Example 1 — Use Ready-Made Adjusters

Jump to the first and last day of the month, to the next Monday, and to the previous Friday.

Java
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;

public class LdtWithAdjusters {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);

        System.out.println("first day:     " + dt.with(TemporalAdjusters.firstDayOfMonth()));
        System.out.println("last day:      " + dt.with(TemporalAdjusters.lastDayOfMonth()));
        System.out.println("next month:    " + dt.with(TemporalAdjusters.firstDayOfNextMonth()));
        System.out.println("next Monday:   " + dt.with(TemporalAdjusters.next(DayOfWeek.MONDAY)));
        System.out.println("previous Fri:  " + dt.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY)));
    }
}

How It Works

  1. One date-time, five rules. Each call starts again from 15 March at 10:30.
  2. First and last day. The result is 1 March and 31 March, and the time stays at 10:30.
  3. First day of next month. That is 1 April.
  4. Next and previous weekday. 15 March is a Sunday, so the next Monday is the 16th and the previous Friday is the 13th.

Example 2 — Set a Field to a Value

Set the hour, the month, and the day of the week with the field version.

Java
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;

public class LdtWithField {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);

        System.out.println("hour 9:      " + dt.with(ChronoField.HOUR_OF_DAY, 9));
        System.out.println("month 12:    " + dt.with(ChronoField.MONTH_OF_YEAR, 12));
        System.out.println("Monday:      " + dt.with(ChronoField.DAY_OF_WEEK, 1));
    }
}

How It Works

  1. Hour 9. The hour changes to 9 and the minutes stay at 30.
  2. Month 12. The date becomes 15 December.
  3. Day of week 1. Monday is day 1 of the ISO week, so the result is the Monday of the same week. The week of Sunday 15 March starts on Monday 9 March.

Example 3 — Replace the Date or the Time

Use a LocalDate or a LocalTime as an adjuster, and try a value that is out of range.

Java
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoField;

public class LdtWithParts {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);

        System.out.println("new date: " + dt.with(LocalDate.of(2030, 1, 1)));
        System.out.println("new time: " + dt.with(LocalTime.of(8, 0)));
        try {
            dt.with(ChronoField.HOUR_OF_DAY, 25);
        } catch (DateTimeException e) {
            System.out.println("hour 25 -> DateTimeException");
        }
    }
}

How It Works

  1. A LocalDate is an adjuster. with(LocalDate) swaps the date and keeps 10:30.
  2. A LocalTime is an adjuster too. with(LocalTime) swaps the time and keeps 15 March.
  3. Out-of-range values fail. Hour 25 does not exist, so Java throws a DateTimeException and the catch block reports it.

Frequently Asked Questions

Use dt.with(TemporalAdjusters.lastDayOfMonth()). It keeps the time of day.
Use dt.with(TemporalAdjusters.next(DayOfWeek.MONDAY)). Use nextOrSame() to include today.
with() is general and takes a field or an adjuster. withHour(), withMonth() and the others are shortcuts for common fields.
No. It returns a new object. LocalDateTime is immutable.
Yes. It is an interface with one method, so a lambda works: dt.with(t -> t.plus(1, ChronoUnit.DAYS)).

Next: withDayOfMonth()

Return a copy with a different day of the month.

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

15 people found this page helpful