Wednesday, 11 April 2018

How to use isSupported method of Instant class | Java 8 Date and Time


Click here to watch on Youtube:
https://www.youtube.com/watch?v=maU8PmGGx8U&list=UUhwKlOVR041tngjerWxVccw

InstantDemo1.java

import java.time.Instant;
import java.time.temporal.ChronoField;

public class InstantDemo1
{
    public static void main(String[] args)
    {
         Instant instant = Instant.parse("2014-12-03T10:15:30.00Z");
       
        /*
         * Parameters:
         *
         * field - the field to check, null returns false
         *
         * Returns:
         *
         * true if the field is supported on this instant, false if
         * not
         */

         System.out.println(instant.isSupported(ChronoField.NANO_OF_SECOND));
         System.out.println(instant.isSupported(ChronoField.DAY_OF_MONTH));
    }

}

Output

true
false

InstantDemo2.java

import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class InstantDemo2
{
    public static void main(String[] args)
    {
        Instant instant = Instant.parse("2014-12-03T10:15:30.00Z");
       
        /*
         * Parameters:
         *
         * unit - the unit to check, null returns false
         *
         * Returns:
         *
         * true if the unit can be added/subtracted, false if not
         */

        System.out.println(instant.isSupported(ChronoUnit.DAYS));
        System.out.println(instant.isSupported(ChronoUnit.WEEKS));
    }

}

Output

true
false

Click the below link to download the code:
https://sites.google.com/site/ramj2eev2/java_basics/InstantDemo_isSuported.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava_2018/InstantDemo_isSuported

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/dce3f55388a87b7da919bb2501377068a8838f7f/BasicJava_2018/InstantDemo_isSuported/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment