Open In App
Related Articles

YearMonth isSupported(TemporalUnit) Method in Java with Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

The isSupported(TemporalUnit) method of YearMonth class is used to Check if the specified TemporalUnit is supported by YearMonth class. Actually, this method checks if we can apply addition or subtraction operation using the passed unit to the specified unit this YearMonth. If false, then calling the plus(long, TemporalUnit) and minus methods will throw an exception.

The supported units of ChronoUnit(Temporalunit) are:

  • MONTHS
  • 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 a boolean value true if the unit is supported on this YearMonth, false if not.

Below programs illustrate the isSupported(TemporalUnit) method:
Program 1:




// Java program to demonstrate
// YearMonth.isSupported(TemporalUnit) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create a YearMonth object
        YearMonth thisYearMonth = YearMonth.of(2017, 8);
  
        // print instance
        System.out.println("YearMonth :"
                           + thisYearMonth);
  
        // apply isSupported() method
        boolean value
            = thisYearMonth.isSupported(
                ChronoUnit.MONTHS);
  
        // print result
        System.out.println("MONTHS unit is supported by YearMonth class: "
                           + value);
    }
}


Output:

YearMonth :2017-08
MONTHS unit is supported by YearMonth class: true

Program 2:




// Java program to demonstrate
// YearMonth.isSupported(TemporalUnit) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a YearMonth object
        YearMonth thisYearMonth = YearMonth.of(2022, 12);
  
        // print instance
        System.out.println("YearMonth :"
                           + thisYearMonth);
  
        // apply isSupported() method
        boolean value
            = thisYearMonth.isSupported(
                ChronoUnit.MILLENNIA);
  
        // print result
        System.out.println("MILLENNIA unit is supported: "
                           + value);
    }
}


Output:

YearMonth :2022-12
MILLENNIA unit is supported: true

References:
https://docs.oracle.com/javase/10/docs/api/java/time/YearMonth.html#isSupported(java.time.temporal.TemporalUnit)


Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 25 Feb, 2019
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials