java.time.MonthDay.withDayOfMonth() Method Example



Description

The java.time.MonthDay.withDayOfMonth(int dayOfMonth) method returns a copy of this MonthDay with the day-of-month altered.

Declaration

Following is the declaration for java.time.MonthDay.withDayOfMonth(int dayOfMonth) method.

public MonthDay withDayOfMonth(int dayOfMonth)

Parameters

dayOfMonth − the day-of-month to set in the return month-day, from 1 to 31.

Return Value

a MonthDay based on this month-day with the requested day, not null.

Exceptions

DateTimeException − if the day-of-month value is invalid, or if the day-of-month is invalid for the month.

Example

The following example shows the usage of java.time.MonthDay.withDayOfMonth(int dayOfMonth) method.

package com.tutorialspoint;

import java.time.MonthDay;

public class MonthDayDemo {
   public static void main(String[] args) {
      
      MonthDay time = MonthDay.parse("--10-15");
      MonthDay result = time.withDayOfMonth(10);
      System.out.println(result);  
   }
}

Let us compile and run the above program, this will produce the following result −

--10-10
Advertisements