Month isSupported() method in Java

The isSupported() method is a built-in method of the Month ENUM which is used to check if the specified field is supported or not. This method accepts a fields as a parameter and returns true or false based on whether the field is supported or not.

Syntax:

public boolean isSupported(TemporalField field)

Parameters: This method accepts a single parameter field, which is to be checked if it is supported or not.

Return Value: This method returns a boolean value True if the field is supported otherwise it returns False.

Below programs illustrate the above method:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.time.*;
import java.time.Month;
import java.time.temporal.ChronoField;
  
class monthEnum {
    public static void main(String[] args)
    {
        // Create a month instance
        Month month = Month.of(5);
  
        // check if the field is valid
        if (month.isSupported(ChronoField.MONTH_OF_YEAR))
            System.out.println("This field is supported!");
        else
            System.out.println("This field is not supported!");
    }
}

chevron_right


Output:

This field is supported!

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.time.*;
import java.time.Month;
import java.time.temporal.ChronoField;
  
class monthEnum {
    public static void main(String[] args)
    {
        // Create a month instance
        Month month = Month.of(5);
  
        // check if the field is valid
        if (month.isSupported(ChronoField.DAY_OF_WEEK))
            System.out.println("This field is supported!");
        else
            System.out.println("This field is not supported!");
    }
}

chevron_right


Output:

This field is not supported!

Reference: https://www.tutorialspoint.com/javatime/javatime_month_issupported.htm

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts: