Last Updated : 17 Dec, 2018
The
range()method of a
ZonedDateTimeclass 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. This ZonedDateTime is helpful to enhance the accuracy of the returned range. When the field is not supported and method is unable to return range values then an exception is thrown.
Syntax:public ValueRange range(TemporalField field)Parameters:
This method accepts one single parameter
fieldwhich is the field to get range of values.
Return value:This method returns
ValueRangewhich is the range of valid values for the field, not null.
Exception:This method throws following Exceptions:
Below programs illustrate the range() method:
Program 1: Java
// Java program to demonstrate
// ZonedDateTime.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 ZonedDateTime object
ZonedDateTime zonedDT
= ZonedDateTime
.parse(
"2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
// print ZonedDateTime
System.out.println("ZonedDateTime of Calcutta: "
+ zonedDT);
// get range of MILLI_OF_SECOND field
// from ZonedDateTime 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);
}
}
Output:
ZonedDateTime of Calcutta: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta] Range of MILLI_OF_SECOND: 0 - 999Program 2: Java
// Java program to demonstrate
// ZonedDateTime.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 ZonedDateTime object
ZonedDateTime zonedDT
= ZonedDateTime
.parse(
"2018-10-25T23:12:31.123+02:00[Europe/Paris]");
// print ZonedDateTime
System.out.println("ZonedDateTime 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);
}
}
Output:
ZonedDateTime of Calcutta: 2018-10-25T23:12:31.123+02:00[Europe/Paris] Range of NANO_OF_SECOND: 0 - 999999999Reference: https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#range(java.time.temporal.TemporalField)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4