Java Instant Class | isSupported() Method with Example

Instant Class isSupported() method: Here, we are going to learn about the isSupported() method of Instant Class with its syntax and example.
Submitted by Preeti Jain, on May 28, 2020

Instant Class isSupported() method

Syntax:

    public boolean isSupported (TemporalField t_field);
    public boolean isSupported (TemporalUnit t_unit);
  • isSupported() method is available in java.time package.
  • isSupported (TemporalField t_field) method is used to identify whether this given field is supported or not on this Instant.
  • isSupported (TemporalUnit t_unit) method is used to identify whether this given unit is supported or not on this Instant.
  • These methods don't throw an exception at the time of checking status.
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, "isSupported (TemporalField t_field)",
    • TemporalField t_field – represents the field to be checked for support.
  • In the second case, "isSupported (TemporalUnit t_unit)",
    • TemporalUnit t_unit – represents the unit to be checked for support.

Return value:

In both the cases, the return type of the method is boolean,

  • In the first case, it returns true when the given field is supported at this Instant otherwise it returns false.
  • In the second case, it returns true when the given unit is supported at this Instant.

Example:

// Java program to demonstrate the example 
// of isSupported() method of Instant

import java.time.*;
import java.time.temporal.*;

public class IsSupportedOfInstant {
    public static void main(String args[]) {
        // Instantiates two Instant
        Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
        Instant ins2 = Instant.now();

        // Display ins1,ins2
        System.out.println("Instant ins1 and ins2: ");
        System.out.println("ins1: " + ins1);
        System.out.println("ins2: " + ins2);

        System.out.println();

        // Here, this method checks whether this
        // Instant (ins1) support the given
        // field or not i.e. here it returns
        // true because ins1 supports 
        // MICRO_OFSECOND
        boolean status = ins1.isSupported(ChronoField.MICRO_OF_SECOND);

        // Display status
        System.out.println("ins1.isSupported(ChronoField.MICRO_OF_SECOND): " + status);

        // Here, this method checks whether this
        // Instant (ins2) supports the given
        // unit or not i.e. here it returns
        // true because ins2 supports 
        // MINUTES
        status = ins2.isSupported(ChronoUnit.MINUTES);

        // Display status
        System.out.println("ins2.isSupported(ChronoUnit.MINUTES): " + status);
    }
}

Output

Instant ins1 and ins2: 
ins1: 2006-04-03T05:10:15Z
ins2: 2020-05-28T00:46:53.435757Z

ins1.isSupported(ChronoField.MICRO_OF_SECOND): true
ins2.isSupported(ChronoUnit.MINUTES): true


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.