Open In App

MonthDay getMonthValue() method in Java with Examples

Last Updated : 06 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
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: Java
// 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: Java
// 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--

Next Article
Practice Tags :

Similar Reads