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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

12

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

1

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

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts: