Java LocalDate withDayOfMonth() Method

Java withDayOfMonth() method is used to get a date with the new day-of-month. It returns a copy of this LocalDate with the day-of-month altered. For example, we have a date 2015-12-14 then setting day-of-month as 10 will result in 2015-12-10.

If the resulting date is invalid, an exception is thrown.

It takes an argument of int type. The syntax of the method is given.


public LocalDate withDayOfMonth(int dayOfMonth)


dayOfMonth - the day-of-month to set in the resulted date.


Returns a LocalDate based on this date with the requested day.

Time for an Example:

Let's take an example to create a new date by setting a new value of a day-of-month. Here, we are using withDayOfMonth() method to set a new day for the date.

import java.time.LocalDate;
public class DateDemo {
	public static void main(String[] args){  
		LocalDate localDate = LocalDate.of(2002, 01, 10);
		localDate = localDate.withDayOfMonth(30);
		System.out.println("New Date : "+localDate);

New Date : 2002-01-30

Live Example:

Try with a live example, execute the code instantly with our powerful Online Java Compiler.