Open In App

MonthDay getMonthValue() method in Java with Examples

The getMonthValue() method of MonthDay class in Java gets the month-of-year field from 1 to 12.

Syntax:



public int getMonthValue()

Parameter: This method accepts no parameters.

Returns: The function returns the month-of-year, from 1 to 12.



Below programs illustrate the MonthDay.getMonthValue() method:

Program 1:




// Program to illustrate the getMonthValue() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Parses the date
        MonthDay tm1 = MonthDay.parse("--12-06");
  
        // Uses the function
        LocalDate dt1 = tm1.atYear(2018);
  
        // Prints the date
        System.out.println(dt1.getMonthValue());
    }
}

Output:
12

Program 2:




// Program to illustrate the getMonthValue() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Parses the date
        MonthDay tm1 = MonthDay.parse("--01-09");
  
        // Uses the function
        LocalDate dt1 = tm1.atYear(2016);
  
        // Prints the date
        System.out.println(dt1.getMonthValue());
    }
}

Output:
1

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/MonthDay.html#getMonthValue–


Article Tags :