Open In App

Month get() method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

The get() method is a built-in method of the Month ENUM which is used to get the corresponding integral value of the month-of-year values specified by this Month instance.

Syntax:

public int get(TemporalField field)

Parameters: This method accepts a single parameter which is a temporal object and cannot be NULL.

Return Value: This method returns an integer corresponding to the field as specified by this Month instance.

Exception: The method throws the following exceptions.

  • DateTimeException: It throws a DateTimeException if the value is outside the range of valid values.
  • ArithmeticException: It throws an ArithmeticException is any numeric overflow occurs.

Below programs illustrate the above method:

Program 1:




import java.time.*;
import java.time.Month;
import java.time.*;
import java.time.temporal.ChronoField;
  
class monthEnum {
    public static void main(String[] args)
    {
        // Create a month instance
        Month month = Month.MARCH;
  
        // Get corresponding Integer
        System.out.println(month.get(ChronoField.MONTH_OF_YEAR));
    }
}


Output:

3

Program 2:




import java.time.*;
import java.time.Month;
import java.time.*;
import java.time.temporal.ChronoField;
  
class monthEnum {
    public static void main(String[] args)
    {
        // Create a month instance
        Month month = Month.NOVEMBER;
  
        // Get corresponding Integer
        System.out.println(month.get(ChronoField.MONTH_OF_YEAR));
    }
}


Output:

11

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Month.html#get-java.time.temporal.TemporalField-



Last Updated : 22 Mar, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads