Year isSupported(TemporalUnit) Method in Java with Examples
Last Updated :
25 Feb, 2019
The isSupported(TemporalUnit) method of Year class is used to Check if the specified TemporalUnit is supported by Year class. Actually, this method checks if we can apply addition or subtraction operation using the passed unit to the specified unit this year. If false, then calling the plus(long, TemporalUnit) and minus methods will throw an exception.
The supported units of ChronoUnit(Temporalunit) are:
- YEARS
- DECADES
- CENTURIES
- MILLENNIA
- ERAS
All other ChronoUnit instances will return false.
Syntax:
public boolean isSupported(TemporalUnit unit)
Parameters: This method accepts only one parameter unit which represents the TemporalUnit to check.
Return Value: This method returns the boolean value true if the unit is supported on this Year, false if not.
Below programs illustrate the isSupported(TemporalUnit) method:
Program 1:
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
Year year = Year.of( 2019 );
System.out.println( "Year :"
+ year);
boolean value
= year.isSupported(
ChronoUnit.YEARS);
System.out.println( "YEARS unit is supported by Year class: "
+ value);
}
}
|
Output:
Year :2019
YEARS unit is supported by Year class: true
Program 2:
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
Year year = Year.of( 2022 );
System.out.println( "Year :"
+ year);
boolean value
= year.isSupported(
ChronoUnit.MILLENNIA);
System.out.println( "MILLENNIA unit is supported: "
+ value);
}
}
|
Output:
Year :2022
MILLENNIA unit is supported: true
References:
https://docs.oracle.com/javase/10/docs/api/java/time/Year.html#isSupported(java.time.temporal.TemporalUnit)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...