The range() method of a ChronoZonedDateTime interface is used to get the range of valid values for the field passes as a parameter. This method returns the ValueRange object which contains the minimum and maximum valid values for a field. This ChronoZonedDateTime is helpful to enhance the accuracy of the returned range. When the field is not supported and the method is unable to return range values then an exception is thrown.
Syntax:
default ValueRange range(TemporalField field)
Parameters: This method accepts one single parameter field which is the field to get range of values.
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 // ChronoZonedDateTime.range() method import java.time.*; import java.time.chrono.*; import java.time.temporal.ChronoField; import java.time.temporal.ValueRange; public class GFG { public static void main(String[] args) { // create a ChronoZonedDateTime object ChronoZonedDateTime zonedDT = ZonedDateTime .parse( "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]"); // print ChronoZonedDateTime System.out.println("ChronoZonedDateTime of Calcutta: " + zonedDT); // get range of MILLI_OF_SECOND field // from ChronoZonedDateTime using range method ValueRange range = zonedDT.range(ChronoField.MILLI_OF_SECOND); // print range of MILLI_OF_SECOND System.out.println("Range of MILLI_OF_SECOND: " + range); } } |
ChronoZonedDateTime of Calcutta: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta] Range of MILLI_OF_SECOND: 0 - 999
Program 2:
// Java program to demonstrate // ChronoZonedDateTime.range() method import java.time.*; import java.time.chrono.*; import java.time.temporal.ChronoField; import java.time.temporal.ValueRange; public class GFG { public static void main(String[] args) { // create a ChronoZonedDateTime object ChronoZonedDateTime zonedDT = ZonedDateTime .parse( "2018-10-25T23:12:31.123+02:00[Europe/Paris]"); // print ChronoZonedDateTime System.out.println("ChronoZonedDateTime of Calcutta: " + zonedDT); // get range of NANO_OF_SECOND field // from zonedDateTime using range method ValueRange range = zonedDT.range(ChronoField.NANO_OF_SECOND); // print range of NANO_OF_SECOND System.out.println("Range of NANO_OF_SECOND: " + range); } } |
ChronoZonedDateTime of Calcutta: 2018-10-25T23:12:31.123+02:00[Europe/Paris] Range of NANO_OF_SECOND: 0 - 999999999
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:
- ChronoZonedDateTime compareTo() method in Java with Examples
- ChronoZonedDateTime isSupported(TemporalUnit) method in Java with Examples
- ChronoZonedDateTime from() method in Java with Examples
- ChronoZonedDateTime equals() method in Java with Examples
- ChronoZonedDateTime format() method in Java with Examples
- ChronoZonedDateTime until() method in Java with Examples
- ChronoZonedDateTime withZoneSameLocal() method in Java with Examples
- ChronoZonedDateTime withLaterOffsetAtOverlap() method in Java with Examples
- ChronoZonedDateTime withZoneSameInstant() method in Java with Examples
- ChronoZonedDateTime toString() method in Java with Examples
- ChronoZonedDateTime withEarlierOffsetAtOverlap() method in Java with Examples
- ChronoZonedDateTime with(TemporalAdjuster) method in Java with Examples
- ChronoZonedDateTime toLocalDateTime() method in Java with Examples
- ChronoZonedDateTime with(TemporalField, long) method in Java with Examples
- ChronoZonedDateTime toLocalTime() method in Java with Examples
- ChronoZonedDateTime toInstant() method in Java with Examples
- ChronoZonedDateTime toLocalDate() method in Java with Examples
- ChronoZonedDateTime plus(TemporalAmount) method in Java with Examples
- ChronoZonedDateTime minus(TemporalAmount) method in Java with Examples
- ChronoZonedDateTime plus(long, TemporalUnit) 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.

