Java LocalDateTime withDayOfMonth() Method

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

Definition

In Java, withDayOfMonth(dayOfMonth) returns a new LocalDateTime with the day of the month set to the value you pass. The year, the month, 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.withDayOfMonth(1);. It is a quick way to jump to the first of the month, or to a fixed billing or payment day.

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 day of the month.

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

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

Library declarations
Java
// java.time.LocalDateTime
public LocalDateTime withDayOfMonth(int dayOfMonth)

Parameters

ParameterDescription
dayOfMonth (int)The new day, from 1 up to the length of the month (28, 29, 30, or 31).

Return Value

Return ValueDescription
LocalDateTimeA new LocalDateTime with the day of 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.
plusDays(n)Adds days to the current day instead of setting one.
withDayOfYear(d)Sets the day by its number from 1 to 365 or 366.

Java Examples

Example 1 — Jump to the First and Last Day

Take 15 March and set the day to 1 and to 31.

Java
import java.time.LocalDateTime;

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

        System.out.println("start: " + start);
        System.out.println("1st:   " + start.withDayOfMonth(1));
        System.out.println("31st:  " + start.withDayOfMonth(31));
    }
}

How It Works

  1. The start. 15 March 2026 at 10:30.
  2. Day 1. withDayOfMonth(1) gives the first day of March, with the same time.
  3. Day 31. March has 31 days, so this is valid and gives 31 March.

Example 2 — Try a Day That Does Not Exist

Set day 31 in April, day 29 in a normal February, and day 29 in a leap-year February.

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

public class LdtWithDayOfMonthInvalid {
    public static void main(String[] args) {
        LocalDateTime april = LocalDateTime.of(2026, 4, 10, 9, 0);
        LocalDateTime feb2026 = LocalDateTime.of(2026, 2, 10, 9, 0);
        LocalDateTime feb2024 = LocalDateTime.of(2024, 2, 10, 9, 0);

        try {
            april.withDayOfMonth(31);
        } catch (DateTimeException e) {
            System.out.println("April 31   -> DateTimeException");
        }
        try {
            feb2026.withDayOfMonth(29);
        } catch (DateTimeException e) {
            System.out.println("Feb 29 2026 -> DateTimeException");
        }
        System.out.println("Feb 29 2024 -> " + feb2024.withDayOfMonth(29));
    }
}

How It Works

  1. April has 30 days. Day 31 does not exist, so Java throws a DateTimeException instead of cutting the day back.
  2. February 2026 has 28 days. Day 29 fails the same way.
  3. February 2024 has 29 days. In a leap year the call is valid and prints 29 February.

Example 3 — Set a Fixed Billing Day

Bills are due on the 28th. Set the day for this month and for next month.

Java
import java.time.LocalDateTime;

public class LdtWithDayOfMonthBilling {
    public static void main(String[] args) {
        LocalDateTime invoice = LocalDateTime.of(2026, 3, 15, 8, 0);

        LocalDateTime dueThisMonth = invoice.withDayOfMonth(28);
        LocalDateTime dueNextMonth = invoice.plusMonths(1).withDayOfMonth(28);

        System.out.println("invoice:    " + invoice);
        System.out.println("due:        " + dueThisMonth);
        System.out.println("next due:   " + dueNextMonth);
    }
}

How It Works

  1. An invoice. It is created on 15 March 2026 at 08:00.
  2. Set the 28th. withDayOfMonth(28) gives 28 March, with the time kept.
  3. Next month first. plusMonths(1) moves to April, and then withDayOfMonth(28) picks the 28th there.
  4. Why 28? Every month has a 28th, so this call can never throw.

Frequently Asked Questions

Call dt.withDayOfMonth(value). It returns a new LocalDateTime with that part replaced and everything else copied.
No. LocalDateTime is immutable, so withDayOfMonth() returns a new object and leaves the original as it was.
A day that does not exist in that month, such as 31 in April or 29 in February of a normal year, throws a DateTimeException. Days below 1 or above 31 always fail.
Call dt.withDayOfMonth(1). It keeps the time, so use toLocalDate() or truncatedTo(ChronoUnit.DAYS) if you want midnight.
Use dt.with(TemporalAdjusters.lastDayOfMonth()). That works for every month and leap year without checking the length yourself.

Next: withDayOfYear()

Return a copy with a different day of the year.

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

7 people found this page helpful