Skip to content
Related Articles

Related Articles

Instant isSupported() method in Java with Examples
  • Last Updated : 18 Dec, 2018

In Instant class, there are two types of isSupported() method depending upon the parameters passed to it.

isSupported(TemporalField field)

isSupported() method of a Instant class used to Check if the specified field is supported by Instant class or not means using this method we can check if this instant can be queried for the specified field.

The supported fields of ChronoField are:

  • NANO_OF_SECOND
  • MICRO_OF_SECOND
  • MILLI_OF_SECOND
  • INSTANT_SECONDS

All other ChronoField instances will return false.

Syntax:



public boolean isSupported(TemporalField field)

Parameters: This method accepts one single parameter field which is the field to check.

Return value: This method returns boolean value. It returns true if the field is supported on this instant, false if not.

Below programs illustrate the isSupported() method:

Program 1:




// Java program to demonstrate
// Instant.isSupported() method
  
import java.time.*;
import java.time.temporal.ChronoField;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a Instant object
        Instant instant
            = Instant.parse("2018-12-30T19:34:50.63Z");
  
        // check Mili of Second value from instant
        boolean value
            = instant.isSupported(
                ChronoField.MILLI_OF_SECOND);
  
        // print result
        System.out.println("MiliSecond Field is supported: "
                           + value);
    }
}
Output:
MiliSecond Field is supported: true

isSupported(TemporalUnit unit)

isSupported() method of a Instant class used to Check if the specified unit is supported by Instant class or not means using this method we can check if this instant can be queried for the specified unit.

The supported fields of ChronoUnit are:

  • NANOS
  • MICROS
  • MILLIS
  • SECONDS
  • MINUTES
  • HOURS
  • HALF_DAYS
  • DAYS

All other ChronoUnit instances will return false.



Syntax:

public boolean isSupported(TemporalUnit unit)

Parameters: This method accepts one single parameter unit which is the unit to check.

Return value: This method returns boolean value true if the field is supported on this instant, false if not.

Below programs illustrate the isSupported() method:

Program 1:




// Java program to demonstrate
// Instant.isSupported() method
  
import java.time.*;
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a Instant object
        Instant instant
            = Instant.parse("2018-12-30T19:34:50.63Z");
  
        // check MILLIS ChronoUnit supported in instant
        boolean value
            = instant.isSupported(ChronoUnit.MILLIS);
  
        // print result
        System.out.println("ChronoUnit MILLIS is  supported: "
                           + value);
    }
}
Output:
ChronoUnit MILLIS is  supported: true

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#isSupported(java.time.temporal.TemporalField), https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#isSupported(java.time.temporal.TemporalUnit)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :