YearMonth isSupported(TemporalField) method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.