ZoneOffset get(TemporalField) method in Java with Examples
The get(TemporalField) method of ZoneOffset Class in java.time package is used to get the value of the specified TemporalField from this ZoneOffset instance. This method takes the TemporalField as the parameter and returns an integer value of this field.
Syntax:
public int get(TemporalField temporalField)
Parameters: This method accepts accepts a parameter temporalField which is required from this ZoneOffset instance.
Return Value: This method returns an integer value which is the field value of the temporalField passed as the parameter to this ZoneOffset instance.
Exceptions: This method throws:
- DateTimeException: if a value for the field cannot be obtained or the value is outside the range of valid values for the field
- UnsupportedTemporalTypeException: if the field is not supported or the range of values exceeds an int
- ArithmeticException: if numeric overflow occurs
Below examples illustrate the ZoneOffset.get() method:
Example 1:
// Java code to illustrate get() method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // Get the ZoneOffset instance ZoneOffset zoneOffset = ZoneOffset.ofHours(5); System.out.println("ZoneOffset: " + zoneOffset); // Using get() method System.out.println("Second value: " + zoneOffset.get(ChronoField.OFFSET_SECONDS)); } } |
ZoneOffset: +05:00 Second value: 18000
Example 2: To show DateTimeException
// Java code to illustrate get() 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(25); System.out.println("ZoneOffset: " + zoneOffset); // Using get() method System.out.println("Second value: " + zoneOffset.get(ChronoField.OFFSET_SECONDS)); } catch (Exception e) { System.out.println(e); } } } |
java.time.DateTimeException: Zone offset hours not in valid range: value 25 is not in the range -18 to 18
Reference: Oracle Doc
Recommended Posts:
- ZoneOffset compareTo(ZoneOffset) method in Java with Examples
- ZoneOffset of(String) method in Java with Examples
- ZoneOffset ofTotalSeconds(int) method in Java with Examples
- ZoneOffset getId() method in Java with Examples
- ZoneOffset toString() method in Java with Examples
- ZoneOffset hashCode() method in Java with Examples
- ZoneOffset ofHours(int) method in Java with Examples
- ZoneOffset ofOffset() method in Java with Examples
- ZoneOffset systemDefault() method in Java with Examples
- ZoneOffset normalized() method in Java with Examples
- ZoneOffset getDisplayName() method in Java with Examples
- ZoneOffset from(TemporalAccessor) method in Java with Examples
- ZoneOffset ofHoursMinutesSeconds(int, int, int) method in Java with Examples
- ZoneOffset ofHoursMinutes(int, int) method in Java with Examples
- ZoneOffset getRules() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


