Home Java LocalDateTime Methods withMonth() Java LocalDateTime withMonth() Method 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.
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 Call What 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.time.LocalDateTime
public LocalDateTime withMonth(int month) Parameters Parameter Description month (int)The new month, from 1 (January) to 12 (December).
Return Value Return Value Description LocalDateTimeA new LocalDateTime with the month replaced. The original is not changed.
Important
If the day does not exist in the new month, Java uses the last valid day of that month: 31 January with withMonth(2) becomes 28 February (29 in a leap year). Months are numbered from 1 , so use Month.DECEMBER.getValue() if you want to be clear.
Compare with Similar Methods Ways to change one part of a date-time:
Java Examples 1. Jump to a month 2. Day is cut back 3. Invalid month Example 1 — Move a Date to Another Month Take 15 March and set the month to December and to January.
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));
}
} start: 2026-03-15T10:30
December: 2026-12-15T10:30
January: 2026-01-15T10:30 How It Works The start. The month is March (3), the day is 15, and the time is 10:30.Set the month to 12. withMonth(12) gives 15 December 2026 and keeps the day and time.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.
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));
}
} February: 2026-02-28T09:00
April: 2026-04-30T09:00
March: 2026-03-31T09:00 How It Works February 2026 has 28 days. There is no 31st, so Java uses 28 February.April has 30 days. The day is cut back to 30 April.March has 31 days. The 31st exists, so nothing is cut back.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.
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()));
}
} withMonth(13) -> DateTimeException
withMonth(0) -> DateTimeException
December is month 12
result: 2026-12-15T10:30 How It Works Month 13 does not exist. withMonth(13) throws a DateTimeException, which the catch block handles.Month 0 does not exist either. Months start at 1, not 0.Use the enum for clarity. Month.DECEMBER.getValue() returns 12.A valid call. The last line sets the month to 12 and prints the new date.Frequently Asked Questions How do I change only the month of a LocalDateTime in Java? Call dt.withMonth(value). It returns a new LocalDateTime with that part replaced and everything else copied.
Does withMonth() change the original LocalDateTime? No. LocalDateTime is immutable, so withMonth() returns a new object and leaves the original as it was.
What happens if the value passed to withMonth() is not valid? A month below 1 or above 12 throws a DateTimeException. Remember that January is 1 and December is 12.
How do I change the month of a LocalDateTime in Java? Use dt.withMonth(6) for June. It returns a new LocalDateTime with the month replaced.
Are Java months numbered from 0 or from 1? From 1. January is 1 and December is 12 in java.time, unlike the old Calendar class where January was 0.
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.
11 people found this page helpful
Helpful Share Copy link Suggestion