Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

OffsetDateTime range() method in Java with Examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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)


My Personal Notes arrow_drop_up
Last Updated : 28 Jan, 2019
Like Article
Save Article
Similar Reads
Related Tutorials