Skip to content
Related Articles

Related Articles

OffsetDateTime range() method in Java with Examples
  • Last Updated : 28 Jan, 2019

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


Output:

Range in CLOCK_HOUR_OF_AMPM: 1 - 12

Program 2:




// Java program to demonstrate
// OffsetDateTime.range() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create OffsetDateTime objects
        OffsetDateTime offsetdateTime
            = OffsetDateTime
                  .parse(
                      "2018-12-12T13:30:30+05:00");
  
        // apply range() method of OffsetDateTime class
        ValueRange result
            = offsetdateTime.range(
                ChronoField.SECOND_OF_DAY);
  
        // print results
        System.out.println("Range in SECOND_OF_DAY: "
                           + result);
    }
}


Output:

Range in SECOND_OF_DAY: 0 - 86399

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :