Wednesday, July 3, 2024
HomeLanguagesJavaZoneOffset range(TemporalField) method in Java with Examples

ZoneOffset range(TemporalField) method in Java with Examples

ZoneOffset range(TemporalField) method in Java with Examples

The range(TemporalField) method of ZoneOffset Class in java.time package is used to get the range of the temporalField, passed as the parameter, in the ZoneOffset. This method returns a ValueRange value stating the same.

Syntax:

public ValueRange range(TemporalField temporalField)

Parameters: This method accepts a parameter temporalField which is the field to query the range for in this ZoneOffset instance. It should not be null.

Return Value: This method returns a ValueRange value stating the range of this temporalField in this ZoneOffset instance.

Exceptions: This method throws:

  • DateTimeException: if the range for the field cannot be obtained.
  • UnsupportedTemporalTypeException: if the field is not supported.

Below examples illustrate the ZoneOffset.range() method:

Example 1:




// Java code to illustrate range() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the ZoneOffset instance
        ZoneOffset zoneOffset
            = ZoneOffset.of("+05:30");
        System.out.println("ZoneOffset: "
                           + zoneOffset);
  
        // Using range() method
        System.out.println("Second value: "
                           + zoneOffset.range(ChronoField.OFFSET_SECONDS));
    }
}


Output:

ZoneOffset: +05:30
Second value: -64800 - 64800

Example 2: To show UnsupportedTemporalTypeException




// Java code to illustrate range() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        try {
            // Get the ZoneOffset instance
            ZoneOffset zoneOffset
                = ZoneOffset.ofHours(5);
            System.out.println("ZoneOffset: "
                               + zoneOffset);
  
            // Using range() method
            System.out.println("Second value: "
                               + zoneOffset.range(ChronoField.NANO_OF_DAY));
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

ZoneOffset: +05:00
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: NanoOfDay

Reference: Oracle Doc

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments