Thursday, June 11, 2026
HomeLanguagesJavaYear range() method in Java with Examples

Year range() method in Java with Examples

range() method of the Year class is used to get the range of field in terms of the minimum and maximum values and the field is passed as a parameter to this method. The returned value for this method is ValueRange object for the field and the method returns ValueRange object only for those fields which are supported by Year object. So when the field is not supported by this method, 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
// Year.range() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create Year object
        Year year
            = Year.of(2017);
  
        // apply range() method of Year class
        ValueRange result
            = year.range(ChronoField.ERA);
  
        // print results
        System.out.println("Range in ERA: "
                           + result);
    }
}


Output:

Range in ERA: 0 - 1

Program 2:




// Java program to demonstrate
// Year.range() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create Year object
        Year year
            = Year.of(2017);
  
        // apply range() method of Year class
        ValueRange result
            = year.range(ChronoField.YEAR);
  
        // print results
        System.out.println("Range in YEAR: "
                           + result);
    }
}


Output:

Range in YEAR: -999999999 - 999999999

References: https://docs.oracle.com/javase/10/docs/api/java/time/Year.html#range(java.time.temporal.TemporalField)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS