Open In App

LocalDateTime isSupported() method in Java

Last Updated : 06 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 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: 

Java




// 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));
    }
}


Output:

2018-11-03T12:45:30
true

Program 2: 

Java




// 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));
    }
}


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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads