Java LocalDate Class | isSupported() Method with Example

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

LocalDate 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 check whether the given field is supported or not.
  • isSupported (TemporalUnit t_unit) method is used to identify whether the given unit is supported or not.
  • 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 class name then we will get an error.

Parameter(s):

  • In the first cases, isSupported (TemporalField t_field),
    • TemporalField t_field – represents the field to be checked for support.
  • In the second cases, atStartOfDay(ZoneId zo),
    • 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 otherwise it returns false.
  • In the second case, it returns true when the given unit is supported otherwise it returns false.

Example:

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

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

public class IsSupportedOfLocalDate {
    public static void main(String args[]) {
        // Instantiates two LocalDate
        LocalDate l_da1 = LocalDate.parse("2007-04-04");
        LocalDate l_da2 = LocalDate.now();

        // Display l_da1,l_da2
        System.out.println("LocalDate l_da1 and l_da2: ");
        System.out.println("l_da1: " + l_da1);
        System.out.println("l_da2: " + l_da2);

        System.out.println();

        // Here, this method checks whether
        // the given field DAY_OF_WEEK is 
        // supported or not
        boolean status = l_da1.isSupported(ChronoField.DAY_OF_WEEK);

        // Display status
        System.out.println("l_da1.isSupported(ChronoField.DAY_OF_WEEK): " + status);

        // Here, this method checks whether
        // the given unit DECADES is supported
        // or not
        status = l_da2.isSupported(ChronoUnit.DECADES);

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

Output

LocalDate l_da1 and l_da2: 
l_da1: 2007-04-04
l_da2: 2020-06-03

l_da1.isSupported(ChronoField.DAY_OF_WEEK): true
l_da2.isSupported(ChronoUnit.DECADES): true



Comments and Discussions!

Load comments ↻






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