Saturday, November 22, 2025
HomeLanguagesJavaYearMonth withMonth() method in Java with Examples

YearMonth withMonth() method in Java with Examples

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.

Syntax:

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:
Program 1:




// Java program to demonstrate
// YearMonth.withMonth() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a YearMonth object
        YearMonth yr
            = YearMonth.of(2019, 12);
  
        // print instance
        System.out.println("YearMonth before"
                           + " applying method: "
                           + yr);
  
        // apply withMonth method of YearMonth class
        YearMonth updatedlocal = yr.withMonth(1);
  
        // print instance
        System.out.println("YearMonth after"
                           + " applying method: "
                           + updatedlocal);
    }
}


Output:

YearMonth before applying method: 2019-12
YearMonth after applying method: 2019-01

Program 2:




// Java program to demonstrate
// YearMonth.withMonth() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a YearMonth object
        YearMonth yr
            = YearMonth.of(2022, 7);
  
        // print instance
        System.out.println("YearMonth before"
                           + " applying method: "
                           + yr);
  
        // apply withMonth method of YearMonth class
        YearMonth updatedlocal = yr.withMonth(12);
  
        // print instance
        System.out.println("YearMonth after"
                           + " applying method: "
                           + updatedlocal);
    }
}


Output:

YearMonth before applying method: 2022-07
YearMonth after applying method: 2022-12

References: https://docs.oracle.com/javase/10/docs/api/java/time/YearMonth.html#withMonth(int)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6784 POSTS0 COMMENTS
Nicole Veronica
11931 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6848 POSTS0 COMMENTS