range() method of the YearMonth class used to get the ValueRange object which is the range of field in terms of the minimum and maximum values for the field passed as a parameter. The method returns ValueRange object only for those fields which are supported by YearMonth object. So when the field is not supported by this method then an exception is thrown by this method.
Syntax:
public ValueRange range(TemporalField field)
Parameters: This method accepts one parameter field which is the field to query the range.
Return value: This method returns the range of valid values for the field.
Exception:This method throws following Exceptions:
- DateTimeException – if the range for the field cannot be obtained.
- UnsupportedTemporalTypeException – if the field is not supported.
Below programs illustrate the range() method:
Program 1:
// Java program to demonstrate // YearMonth.range() method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create YearMonth object YearMonth yearMonth = YearMonth.of(2017, 8); // apply range() method of YearMonth class ValueRange result = yearMonth.range(ChronoField.YEAR_OF_ERA); // print results System.out.println("Range in YEAR_OF_ERA: " + result); } } |
Range in YEAR_OF_ERA: 1 - 999999999
Program 2:
// Java program to demonstrate // YearMonth.range() method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create YearMonth object YearMonth yearMonth = YearMonth.of(2017, 8); // apply range() method of YearMonth class ValueRange result = yearMonth.range(ChronoField.ERA); // print results System.out.println("Range in ERA: " + result); } } |
Range in ERA: 0 - 1
References: https://docs.oracle.com/javase/10/docs/api/java/time/YearMonth.html#range(java.time.temporal.TemporalField)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- YearMonth isSupported(TemporalUnit) Method in Java with Examples
- YearMonth lengthOfYear() method in Java with Examples
- YearMonth isValidDay() method in Java with Examples
- YearMonth isLeapYear() method in Java with Examples
- YearMonth isBefore() method in Java with Examples
- YearMonth isAfter() method in Java with Examples
- YearMonth query() Method in Java with Examples
- YearMonth with() Method in Java with Examples
- YearMonth withYear() method in Java with Examples
- YearMonth withMonth() method in Java with Examples
- YearMonth until() Method in Java with Examples
- YearMonth plus(long,unit) method in Java with Examples
- YearMonth plus(TemporalAmount) method in Java with Examples
- YearMonth minus(TemporalAmount) method in Java with Examples
- YearMonth isSupported(TemporalField) method in Java with Examples
- YearMonth minus(long,unit) method in Java with Examples
- YearMonth parse(CharSequence) method in Java with Examples
- YearMonth parse(CharSequence,DateTimeFormatter) method in Java with Examples
- YearMonth plusMonths() method in Java with Examples
- YearMonth 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.

