ZoneOffset getLong(TemporalField) method in Java with Examples

The getLong(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 long value of this field. Syntax:
public long getLong(TemporalField temporalField)
Parameters: This method accepts  a parameter temporalField which is required from this ZoneOffset instance. Return Value: This method returns an long 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.getLong() method: Example 1:Â
Java
// Java code to illustrate getLong() 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 getLong() method        System.out.println("Second value: "                           + zoneOffset.getLong(ChronoField.OFFSET_SECONDS));    }} |
Output:
ZoneOffset: +05:30 Second value: 19800
Example 2: To show DateTimeExceptionÂ
Java
// Java code to illustrate getLong() 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 getLong() method            System.out.println("Second value: "                               + zoneOffset.getLong(ChronoField.OFFSET_SECONDS));        }        catch (Exception e) {            System.out.println(e);        }    }} |
Output:
java.time.DateTimeException: Zone offset hours not in valid range: value 25 is not in the range -18 to 18
Reference: Oracle Doc



