MonthDay withDayOfMonth() Method in Java with Examples
withDayOfMonth(int dayOfMonth) method of the MonthDay class used to alter the day-of-month of MonthDay object using dayOfMonth passed as a parameter and after that method returns the copy of altered MonthDay.An exception is thrown If the day-of-month value is invalid for the specified month after altering operation.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public MonthDay withDayOfMonth(int dayOfMonth)
Parameters: This method accepts dayOfMonth as parameter which is the day-of-month to set in the return month-day, from 1 to 31.
Return value: This method returns a MonthDay based on this month-day with the requested day.
Exception: This method throws DateTimeException if the day-of-month value is invalid, or if the day-of-month is invalid for the month.
Below programs illustrate the withDayOfMonth() method:
MonthDay before applying method: --08-28 MonthDay after applying method: --08-21
MonthDay before applying method: --10-31 MonthDay after applying method: --10-12