Signup/Sign In
LAST UPDATED: NOVEMBER 24, 2020

Java LocalDate isSupported() Method

Java isSupported() method is used to check if the specified date unit is supported or not. The date unit is part of the date like day, month, year, etc. For example, if we want to check a month's field on the date then we can use this method.

This method takes an argument and returns either true or false. If the specified field is supported then it returns true, false otherwise.

There are the supported fields:

  • DAY_OF_WEEK

  • ALIGNED_DAY_OF_WEEK_IN_MONTH

  • ALIGNED_DAY_OF_WEEK_IN_YEAR

  • DAY_OF_MONTH

  • DAY_OF_YEAR

  • EPOCH_DAY

  • ALIGNED_WEEK_OF_MONTH

  • ALIGNED_WEEK_OF_YEAR

  • MONTH_OF_YEAR

  • PROLEPTIC_MONTH

  • YEAR_OF_ERA

  • YEAR

  • ERA

Except for the above units, all other ChronoField instances will return false.

Syntax

public boolean isSupported(TemporalField field)

Parameters:

It takes a single parameter as a date's field.

Returns:

It returns true if the specified field is supported, false otherwise.

Time for an Example:

Let's take an example, to check whether the specified field is supported by the date. Here, we are using DAY_OF_MONTH as a field and the output is true.

import java.time.LocalDate;
import java.time.temporal.ChronoField; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2020, 10, 21);
		System.out.println(localDate);
		boolean d  = localDate.isSupported(ChronoField.DAY_OF_MONTH);
		System.out.println("Is day of Month Supported : "+d);
	}
}

2020-10-21
Is day of Month Supported : true

Time for another Example:

Let's take another example to understand the use of isSupported() method. Here, we are using HOUR_OF_DAY field which is not supported by the specified date so that we get false as a result.

import java.time.LocalDate;
import java.time.temporal.ChronoField; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2020, 10, 21);
		System.out.println(localDate);
		boolean d  = localDate.isSupported(ChronoField.HOUR_OF_DAY);
		System.out.println("Is Hour of Day Supported : "+d);
	}
}


2020-10-21
Is Hour of Day Supported : false

Live Example:

Try with a live example, execute the code instantly with our powerful Online Java Compiler.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.