Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

MonthDay withDayOfMonth() Method in Java with Examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.

Syntax:

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




// Java program to demonstrate
// MonthDay.withDayOfMonth() 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 withDayOfMonth method
        // of MonthDay class
        MonthDay updatedlocal
            = monthday.withDayOfMonth(21);
  
        // print instance
        System.out.println("MonthDay after"
                           + " applying method: "
                           + updatedlocal);
    }
}


Output:

MonthDay before applying method: --08-28
MonthDay after applying method: --08-21

Program 2:




// Java program to demonstrate
// MonthDay.withDayOfMonth() 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 withDayOfMonth method
        // of MonthDay class
        MonthDay updatedlocal
            = monthday.withDayOfMonth(12);
  
        // print instance
        System.out.println("MonthDay after"
                           + " applying method: "
                           + updatedlocal);
    }
}


Output:

MonthDay before applying method: --10-31
MonthDay after applying method: --10-12

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


My Personal Notes arrow_drop_up
Last Updated : 15 Jan, 2019
Like Article
Save Article
Similar Reads
Related Tutorials