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

LocalDateTime range() method in Java with Examples

range() method of the LocalDateTime class is useful to get the minimum and maximum values as a ValueRange object for the field passed as a parameter to this method. This method returns ValueRange object only for those fields which are supported by LocalDate 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
// LocalDateTime.range() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create LocalDateTime objects
        LocalDateTime l1
            = LocalDateTime
                  .parse("2018-12-06T19:21:12");
  
        // apply range() method of LocalDateTime class
        ValueRange result
            = l1.range(ChronoField.MILLI_OF_SECOND);
  
        // print results
        System.out.println("Range in MILLI_OF_SECOND: "
                           + result);
    }
}


Output:

Range in MILLI_OF_SECOND: 0 - 999

Program 2:




// Java program to demonstrate
// LocalDateTime.range() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create LocalDateTime objects
        LocalDateTime l1
            = LocalDateTime
                  .parse("2018-12-06T19:21:12");
  
        // apply range() method of LocalDateTime class
        ValueRange result
            = l1.range(ChronoField.DAY_OF_YEAR);
  
        // print results
        System.out.println("Range in DAY_OF_YEAR: "
                           + result);
    }
}


Output:

Range in DAY_OF_YEAR: 1 - 365

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