MonthDay isSupported() Method in Java

Java 8Object Oriented ProgrammingProgramming

<p>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.</p><p>A program that demonstrates this is given as follows</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/nnvjfH" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">import java.time.*; import java.time.temporal.*; public class Demo { &nbsp; &nbsp;public static void main(String[] args) { &nbsp; &nbsp; &nbsp; MonthDay md = MonthDay.now(); &nbsp; &nbsp; &nbsp; System.out.println(&quot;The MonthDay is: &quot; + md); &nbsp; &nbsp; &nbsp; boolean flag = md.isSupported(ChronoField.MONTH_OF_YEAR); &nbsp; &nbsp; &nbsp; if(flag) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot;The ChronoField MONTH_OF_YEAR is supported&quot;); &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot;The ChronoField MONTH_OF_YEAR is not supported&quot;); &nbsp; &nbsp;} }</pre><h2>output</h2><pre class="result notranslate">The MonthDay is: --02-21 The ChronoField MONTH_OF_YEAR is supported</pre><p>Now let us understand the above program.</p><p>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:</p><pre class="prettyprint notranslate">MonthDay md = MonthDay.now(); System.out.println(&quot;The MonthDay is: &quot; + md); boolean flag = md.isSupported(ChronoField.MONTH_OF_YEAR); if(flag) &nbsp; &nbsp;System.out.println(&quot;The ChronoField MONTH_OF_YEAR is supported&quot;); else &nbsp; &nbsp;System.out.println(&quot;The ChronoField MONTH_OF_YEAR is not supported&quot;);</pre>
raja
Updated on 30-Jul-2019 22:30:25

Advertisements