MonthDay withMonth() Method in Java with Examples Last Updated : 15 Jan, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report withMonth(int month) method of the MonthDay class used to alter the month-of-year of MonthDay object using month passed as a parameter and after that method returns the copy of altered MonthDay.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. Syntax: public MonthDay withMonth(int month) Parameters: This method accepts month as parameter which is the month-of-year to set in the returned month-day, from 1 (January) to 12 (December). Return value: This method returns a MonthDay based on this month-day with the requested month. Exception: This method throws DateTimeException if the month-of-year value is invalid. Below programs illustrate the withMonth() method: Program 1: Java // Java program to demonstrate // MonthDay.withMonth() method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create a MonthDay object MonthDay monthday = MonthDay.of(8, 28); // print instance System.out.println("MonthDay before" + " applying method: " + monthday); // apply withMonth method of MonthDay class MonthDay updatedlocal = monthday.withMonth(1); // print instance System.out.println("MonthDay after" + " applying method: " + updatedlocal); } } Output: MonthDay before applying method: --08-28 MonthDay after applying method: --01-28 Program 2: Java // Java program to demonstrate // MonthDay.withMonth() method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create a MonthDay object MonthDay monthday = MonthDay.of(10, 31); // print instance System.out.println("MonthDay before" + " applying method: " + monthday); // apply withMonth method of MonthDay class MonthDay updatedlocal = monthday.withMonth(5); // print instance System.out.println("MonthDay after" + " applying method: " + updatedlocal); } } Output: MonthDay before applying method: --10-31 MonthDay after applying method: --05-31 References: https://docs.oracle.com/javase/10/docs/api/java/time/MonthDay.html#withMonth(int) Comment More infoAdvertise with us Next Article MonthDay withMonth() Method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Java-time package Java-MonthDay Practice Tags : Java Similar Reads 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 al 2 min read MonthDay with() Method in Java with Examples with(Month month) method of the MonthDay class used to alter the month-of-year of MonthDay object using month passed as a parameter and after that method returns the copy of altered MonthDay.If the day-of-month value is invalid for the specified month after altering operation, the day will be adjust 2 min read MonthDay now() method in Java with Examples The now() method of the MonthDay class in Java is used to get the current month-day from the system clock in the default time-zone. Syntax: public static MonthDay now() Parameters: This method does not accept any parameter. Return value: This method returns the current month-day using the system clo 1 min read Period withMonths() method in Java with Examples The withMonths() method of Period Class is used to obtain a period with specified number of months. This number of months is passed as the parameter as integer value. Syntax: public Period withMonths(int numberOfMonths) Parameters: This method accepts a parameter numberOfMonths which is the number o 2 min read OffsetDateTime withMonth() method in Java with examples The withMonth() method of OffsetDateTime class in Java returns a copy of this OffsetDateTime with the month-of-year altered as specified in the parameter. Syntax: public OffsetDateTime withMonth(int month) Parameter: This method accepts a single parameter month which specifies the month-of-year to b 2 min read Like