Saturday, October 25, 2025
HomeLanguagesJavaLocalDate range() method in Java with Examples

LocalDate range() method in Java with Examples

The range() method of a LocalDate class is used to get the range of valid values for the field passes as a parameter. This method returns ValueRange object which contains the minimum and maximum valid values for a field. When the field is not supported then an exception is thrown. This LocalDate is helpful to enhance the accuracy of the returned range.

Syntax:

public ValueRange range(TemporalField field)

Parameters: This method accepts one single parameter field which is the field to query the range for.

Return value: This method returns ValueRange which is the range of valid values for the field, not null.

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
// LocalDate.range() method
  
import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDate object
        LocalDate localD
            = LocalDate.parse("2018-12-06");
  
        // print LocalDate
        System.out.println("LocalDate: "
                           + localD);
  
        // get range of Days field
        // from LocalDate using range method
        ValueRange range
            = localD.range(ChronoField.DAY_OF_MONTH);
  
        // print range of DAY_OF_MONTH
        System.out.println("Range of DAY_OF_MONTH: "
                           + range);
    }
}


Output:

LocalDate: 2018-12-06
Range of DAY_OF_MONTH: 1 - 31

Program 2:




// Java program to demonstrate
// LocalDate.range() method
  
import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDate object
        LocalDate localD
            = LocalDate.parse("2018-12-06");
  
        // print LocalDate
        System.out.println("LocalDate: "
                           + localD);
  
        // get range of DAY_OF_YEAR field
        // from LocalDate using range method
        ValueRange range
            = localD.range(ChronoField.DAY_OF_YEAR);
  
        // print range of DAY_OF_YEAR
        System.out.println("Range of DAY_OF_YEAR: "
                           + range);
    }
}


Output:

LocalDate: 2018-12-06
Range of DAY_OF_YEAR: 1 - 365

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.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

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS