LocalDateTime isSupported() method in Java

The isSupported() method of LocalDateTime class in Java checks if the specified unit or field is supported.

Syntax:

public boolean isSupported(TemporalUnit unit)
           or
public boolean isSupported(TemporalField field)

Parameter: This method accepts a parameter field which specifies the field to check, null returns false or it accepts a parameter unit which specifies the the unit to check, null returns false.



Returns: The function returns true if the unit of field is supported on this date, false if not.

Below programs illustrate the LocalDateTime.isSupported() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the isSupported(TemporalUnit) method
  
import java.util.*;
import java.time.*;
import java.time.temporal.ChronoUnit;
  
public class GfG {
    public static void main(String[] args)
    {
        // Parses the date
        LocalDateTime dt1
            = LocalDateTime.parse("2018-11-03T12:45:30");
  
        // Prints the date
        System.out.println(dt1);
  
        System.out.println(dt1.isSupported(ChronoUnit.DAYS));
    }
}

chevron_right


Output:

2018-11-03T12:45:30
true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the isSupported(TemporalField) method
  
import java.util.*;
import java.time.*;
import java.time.temporal.ChronoField;
  
public class GfG {
    public static void main(String[] args)
    {
        // Parses the date
        LocalDateTime dt1
            = LocalDateTime.parse("2018-11-03T12:45:30");
  
        // Prints the date
        System.out.println(dt1);
  
        System.out.println(
            dt1
                .isSupported(ChronoField.DAY_OF_WEEK));
    }
}

chevron_right


Output:

2018-11-03T12:45:30
true

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



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2