Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaOffsetTime range() method in Java with Examples

OffsetTime range() method in Java with Examples

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


Output:

Range in OFFSET_SECONDS: -64800 - 64800

Program 2:




// Java program to demonstrate
// OffsetTime.range() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create OffsetTime objects
        OffsetTime offset
            = OffsetTime
                  .parse("15:30:30+07:00");
  
        // apply range() method of OffsetTime class
        ValueRange result
            = offset.range(ChronoField.SECOND_OF_MINUTE);
  
        // print results
        System.out.println("Range in SECOND_OF_MINUTE: "
                           + result);
    }
}


Output:

Range in SECOND_OF_MINUTE: 0 - 59

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

RELATED ARTICLES

Most Popular

Recent Comments