ZoneOffset of(String) method in Java with Examples - GeeksforGeeks

11 Dec, 2018
The of(String) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offsetId passed as the parameter. This method takes the offsetId as parameter in the form of String and converts it into the ZoneOffset. The ID of the returned offset will be normalized to one of the formats described by getId().The list of String offsetId accepted by this method are as follows:
Z – for UTC
+h
+hh
+hh:mm
-hh:mm
+hhmm
-hhmm
+hh:mm:ss
-hh:mm:ss
+hhmmss
-hhmmssNote: ± means either the plus or minus symbol. And the maximum supported range is from +18:00 to -18:00 inclusive.Syntax:public static ZoneOffset of(String offsetId)
Parameters: This method accepts a parameter offsetId which is String to be parsed into an ZoneOffset instance.Return Value: This method returns a ZoneOffset instance parsed from the specified offsetId.Exception: This method throws DateTimeException if the offset ID is invalid.Below examples illustrate the ZoneOffset.of() method:Example 1:// Java code to illustrate of() method  import java.time.*;  public class GFG {    public static void main(String[] args)    {          // Get the offset ID        String offsetId = "Z";          // ZoneOffset using of() method        ZoneOffset zoneOffset            = ZoneOffset.of(offsetId);          System.out.println(zoneOffset);    }}Output:Z
Example 2: To demonstrate DateTimeException// Java code to illustrate of() method  import java.time.*;  public class GFG {    public static void main(String[] args)    {          // Get the invalid offset ID        String offsetId = "10:10";          try {            // ZoneOffset using of() method            ZoneOffset zoneOffset                = ZoneOffset.of(offsetId);        }          catch (Exception e) {            System.out.println(e);        }    }}Output:java.time.DateTimeException: Invalid ID for ZoneOffset, non numeric characters found: 10:10
Reference: Oracle DocMy Personal Notes
arrow_drop_upSave
Note: ± means either the plus or minus symbol. And the maximum supported range is from +18:00 to -18:00 inclusive.
Syntax:
public static ZoneOffset of(String offsetId)

Parameters: This method accepts a parameter offsetId which is String to be parsed into an ZoneOffset instance.
Return Value: This method returns a ZoneOffset instance parsed from the specified offsetId.
Exception: This method throws DateTimeException if the offset ID is invalid.
Below examples illustrate the ZoneOffset.of() method:
Example 1:
// Java code to illustrate of() method  import java.time.*;  public class GFG {    public static void main(String[] args)    {          // Get the offset ID        String offsetId = "Z";          // ZoneOffset using of() method        ZoneOffset zoneOffset            = ZoneOffset.of(offsetId);          System.out.println(zoneOffset);    }}
Z

Example 2: To demonstrate DateTimeException
// Java code to illustrate of() method  import java.time.*;  public class GFG {    public static void main(String[] args)    {          // Get the invalid offset ID        String offsetId = "10:10";          try {            // ZoneOffset using of() method            ZoneOffset zoneOffset                = ZoneOffset.of(offsetId);        }          catch (Exception e) {            System.out.println(e);        }    }}
java.time.DateTimeException: Invalid ID for ZoneOffset, non numeric characters found: 10:10

Reference: Oracle Doc
Java-Functions
Java-time package
Java-ZoneOffset
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
HashMap in Java with Examples
Interfaces in Java
Object Oriented Programming (OOPs) Concept in Java
ArrayList in Java
How to iterate any Map in Java
Initialize an ArrayList in Java
Singleton Class in Java
Overriding in Java
Collections in Java
Multithreading in Java