MonthDay isSupported() Method in Java


It can be checked if a ChronoField is supported by the MonthDay class or not by using the isSupported() method in the MonthDay class in Java. This method requires a single parameter i.e. the ChronoField to check. It returns true if the ChronoField is supported by the MonthDay class and false otherwise.

A program that demonstrates this is given as follows

Example

 Live Demo

import java.time.*;
import java.time.temporal.*;
public class Demo {
   public static void main(String[] args) {
      MonthDay md = MonthDay.now();
      System.out.println("The MonthDay is: " + md);
      boolean flag = md.isSupported(ChronoField.MONTH_OF_YEAR);
      if(flag)
         System.out.println("The ChronoField MONTH_OF_YEAR is supported");
      else
         System.out.println("The ChronoField MONTH_OF_YEAR is not supported");
   }
}

output

The MonthDay is: --02-21
The ChronoField MONTH_OF_YEAR is supported

Now let us understand the above program.

First the current MonthDay object is displayed. Then the isSupported() method is used to check if the ChronoField MONTH_OF_YEAR is supported by the MonthDay class or not. The returned value is stored in flag and the appropriate response is printed. A code snippet that demonstrates this is as follows:

MonthDay md = MonthDay.now();
System.out.println("The MonthDay is: " + md);
boolean flag = md.isSupported(ChronoField.MONTH_OF_YEAR);
if(flag)
   System.out.println("The ChronoField MONTH_OF_YEAR is supported");
else
   System.out.println("The ChronoField MONTH_OF_YEAR is not supported");

Updated on: 30-Jul-2019

51 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements