Skip to content
Related Articles

Related Articles

YearMonth isSupported(TemporalField) method in Java with Examples

View Discussion
Improve Article
Save Article
  • Last Updated : 24 Jul, 2019
View Discussion
Improve Article
Save Article

The isSupported(TemporalField) method of YearMonth class is used to check if the specified field is supported by YearMonth class or not means using this method we can check if this YearMonth object can be queried for the specified field.

The supported fields of ChronoField are:

  • MONTH_OF_YEAR
  • PROLEPTIC_MONTH
  • YEAR_OF_ERA
  • YEAR
  • ERA

All other ChronoField instances will return false.

Syntax:

public boolean isSupported(TemporalField field)

Parameters: This method accepts only one parameter field which represents the field to check.

Return Value: This method returns the boolean value true if the field is supported on this YearMonth, false if not.

Below programs illustrate the isSupported(TemporalField) method:

Program 1:




// Java program to demonstrate
// YearMonth.isSupported(TemporalField) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create a YearMonth object
        YearMonth thisYearMonth = YearMonth.of(2017, 8);
  
        // print instance
        System.out.println("YearMonth :"
                           + thisYearMonth);
  
        // apply isSupported() method
        boolean value
            = thisYearMonth.isSupported(
                ChronoField.PROLEPTIC_MONTH);
  
        // print result
        System.out.println("PROLEPTIC_MONTH Field is supported by YearMonth class: "
                           + value);
    }
}


Output:

YearMonth :2017-08
PROLEPTIC_MONTH Field is supported by YearMonth class: true

Program 2:




// Java program to demonstrate
// YearMonth.isSupported(TemporalField) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create a YearMonth object
        YearMonth thisYearMonth = YearMonth.of(2017, 8);
  
        // print instance
        System.out.println("YearMonth :"
                           + thisYearMonth);
  
        // apply isSupported() method
        boolean value
            = thisYearMonth.isSupported(
                ChronoField.HOUR_OF_DAY);
  
        // print result
        System.out.println("HOUR_OF_DAY Field is supported by YearMonth class: "
                           + value);
    }
}


Output:

YearMonth :2017-08
HOUR_OF_DAY Field is supported by YearMonth class: false

References:
https://docs.oracle.com/javase/10/docs/api/java/time/YearMonth.html#isSupported(java.time.temporal.TemporalField)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!