Java LocalDateTime withMonth() Method

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

Definition

In Java, withMonth(month) returns a new LocalDateTime with the month set to the value you pass. The year, the day, and the time of day are copied from the original.

LocalDateTime is immutable, so the original object never changes. Always keep the result: dt = dt.withMonth(12);. The month is a number from 1 (January) to 12 (December), not 0 to 11.

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 month.

Java
LocalDateTime start = LocalDateTime.of(2026, 3, 15, 10, 30);
LocalDateTime result = start.withMonth(12);   // 2026-12-15T10:30
System.out.println(start);   // 2026-03-15T10:30
CallWhat it does
dt.withMonth(n)Returns a copy with the month replaced by n.
dt.with(ChronoField.MONTH_OF_YEAR, n)The general form for any field. It does the same thing here.
dt.plusMonths(n)Moves by an amount instead of setting a value.
date.withMonth(m)LocalDate has the same method, but it has no time of day.

The result is a new object. Everything except the month is copied from the original.

Library declarations
Java
// java.time.LocalDateTime
public LocalDateTime withMonth(int month)

Parameters

ParameterDescription
month (int)The new month, from 1 (January) to 12 (December).

Return Value

Return ValueDescription
LocalDateTimeA new LocalDateTime with the month 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.
plusMonths(n)Adds months to the current month instead of setting one.
withDayOfMonth(d)Sets the day of the month and keeps the month.

Java Examples

Example 1 — Move a Date to Another Month

Take 15 March and set the month to December and to January.

Java
import java.time.LocalDateTime;

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

        System.out.println("start:    " + start);
        System.out.println("December: " + start.withMonth(12));
        System.out.println("January:  " + start.withMonth(1));
    }
}

How It Works

  1. The start. The month is March (3), the day is 15, and the time is 10:30.
  2. Set the month to 12. withMonth(12) gives 15 December 2026 and keeps the day and time.
  3. Set the month to 1. January 2026 comes before the start, because the method sets a value instead of adding months.

Example 2 — See the Day Cut Back to Fit

Start on 31 January and set the month to February, April, and March.

Java
import java.time.LocalDateTime;

public class LdtWithMonthClamp {
    public static void main(String[] args) {
        LocalDateTime jan31 = LocalDateTime.of(2026, 1, 31, 9, 0);

        System.out.println("February: " + jan31.withMonth(2));
        System.out.println("April:    " + jan31.withMonth(4));
        System.out.println("March:    " + jan31.withMonth(3));
    }
}

How It Works

  1. February 2026 has 28 days. There is no 31st, so Java uses 28 February.
  2. April has 30 days. The day is cut back to 30 April.
  3. March has 31 days. The 31st exists, so nothing is cut back.
  4. No exception. Java chooses the last valid day, so the code keeps running.

Example 3 — Try Months That Do Not Exist

Set the month to 13, to 0, and to the value of Month.DECEMBER.

Java
import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.Month;

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

        try {
            start.withMonth(13);
        } catch (DateTimeException e) {
            System.out.println("withMonth(13) -> DateTimeException");
        }
        try {
            start.withMonth(0);
        } catch (DateTimeException e) {
            System.out.println("withMonth(0)  -> DateTimeException");
        }

        System.out.println("December is month " + Month.DECEMBER.getValue());
        System.out.println("result: " + start.withMonth(Month.DECEMBER.getValue()));
    }
}

How It Works

  1. Month 13 does not exist. withMonth(13) throws a DateTimeException, which the catch block handles.
  2. Month 0 does not exist either. Months start at 1, not 0.
  3. Use the enum for clarity. Month.DECEMBER.getValue() returns 12.
  4. A valid call. The last line sets the month to 12 and prints the new date.

Frequently Asked Questions

Call dt.withMonth(value). It returns a new LocalDateTime with that part replaced and everything else copied.
No. LocalDateTime is immutable, so withMonth() returns a new object and leaves the original as it was.
A month below 1 or above 12 throws a DateTimeException. Remember that January is 1 and December is 12.
Use dt.withMonth(6) for June. It returns a new LocalDateTime with the month replaced.
From 1. January is 1 and December is 12 in java.time, unlike the old Calendar class where January was 0.

Next: withNano()

Return a copy with a different fraction of a second.

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

11 people found this page helpful