java.time.Month.get() Method Example



Description

The java.time.Month.get(TemporalField field) method gets the value of the specified field from this Month as an int.

Declaration

Following is the declaration for java.time.Month.get(TemporalField field) method.

public int get(TemporalField field)

Parameters

field − the field to get, not null.

Return Value

the value for the field.

Exceptions

  • DateTimeException − if a value for the field cannot be obtained or the value is outside the range of valid values for the field.

  • UnsupportedTemporalTypeException − if the field is not supported or the range of values exceeds an int.

  • ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.Month.get(TemporalField field) method.

package com.tutorialspoint;

import java.time.Month;
import java.time.temporal.ChronoField;

public class MonthDemo {
   public static void main(String[] args) {

      Month day = Month.FEBRUARY;
      System.out.println(day.get(ChronoField.MONTH_OF_YEAR));
   }
}

Let us compile and run the above program, this will produce the following result −

2
Advertisements