Year isSupported(TemporalUnit) Method in Java with Examples
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:
// Java program to demonstrate // Year.isSupported(TemporalUnit) method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create a Year object Year year = Year.of(2019); // print instance System.out.println("Year :" + year); // apply isSupported() method boolean value = year.isSupported( ChronoUnit.YEARS); // print result System.out.println("YEARS unit is supported by Year class: " + value); } } |
Year :2019 YEARS unit is supported by Year class: true
Program 2:
// Java program to demonstrate // Year.isSupported(TemporalUnit) method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create a Year object Year year = Year.of(2022); // print instance System.out.println("Year :" + year); // apply isSupported() method boolean value = year.isSupported( ChronoUnit.MILLENNIA); // print result System.out.println("MILLENNIA unit is supported: " + value); } } |
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)
Recommended Posts:
- Year now() method in Java with examples
- Year with() Method in Java with Examples
- Year get() method in Java with Examples
- Year from() Method in Java with Examples
- Year of() method in Java with Examples
- Year until() Method in Java with Examples
- Year format() method in Java with Examples
- Year toString() method in Java with Examples
- Year getValue() method in Java with Examples
- Year equals() method in Java with Examples
- Year compareTo() method in Java with Examples
- Year query() Method in Java with Examples
- Year minusYears() method in Java with Examples
- Year adjustInto() Method in Java with Examples
- Year plusYears() Method in Java with Examples
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.



