withMonth(int month) method of the YearMonth class used to alter the month-of-year of YearMonth object using month passed as a parameter and after that method returns the copy of altered YearMonth.If the day-of-month is invalid for the specified month, the day will be adjusted to the last valid day-of-month. This instance is immutable and unaffected by this method call.
public YearMonth withMonth(int month)
Parameters: This method accepts month as parameter which is the month-of-year to set in the returned year-month, from 1 (January) to 12 (December).
Return value: This method returns a YearMonth based on this year-month with the requested month.
Exception: This method throws DateTimeException if the month-of-year value is invalid.
Below programs illustrate the withMonth() method:
YearMonth before applying method: 2019-12 YearMonth after applying method: 2019-01
YearMonth before applying method: 2022-07 YearMonth after applying method: 2022-12
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.