LocalTime isSupported() method in Java

Java 8Object Oriented ProgrammingProgramming

<p>It can be checked if a ChronoUnit is supported by the LocalTime class or not by using the isSupported() method in the LocalTime class in Java. This method requires a single parameter i.e. the ChronoUnit to check. It returns true if the ChronoUnit is supported by the LocalTime class and false otherwise.</p><p>A program that demonstrates this is given as follows &minus;</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/PhRXSX" 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; LocalTime lt = LocalTime.now(); &nbsp; &nbsp; &nbsp; System.out.println(&quot;The LocalTime is: &quot; + lt); &nbsp; &nbsp; &nbsp; boolean flag = lt.isSupported(ChronoUnit.HOURS); &nbsp; &nbsp; &nbsp; if(flag) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot;The ChronoUnit HOURS is supported&quot;); &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(&quot;The ChronoUnit HOURS is not supported&quot;); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">The LocalTime is: 07:09:42.262 The ChronoUnit HOURS is supported</pre><p>Now let us understand the above program.</p><p>First the current LocalTime object is displayed. Then the isSupported() method is used to check if the ChronoUnit HOURS is supported by the LocalTime 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 &minus;</p><pre class="prettyprint notranslate">LocalTime lt = LocalTime.now(); System.out.println(&quot;The LocalTime is: &quot; + lt); boolean flag = lt.isSupported(ChronoUnit.HOURS); if(flag) &nbsp; &nbsp;System.out.println(&quot;The ChronoUnit HOURS is supported&quot;); else &nbsp; &nbsp;System.out.println(&quot;The ChronoUnit HOURS is not supported&quot;);</pre>
raja
Updated on 30-Jul-2019 22:30:25

Advertisements