Saturday, September 6, 2025
HomeLanguagesJavaMonthDay withMonth() Method in Java with Examples

MonthDay withMonth() Method in Java with Examples

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 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 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)

RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS