ZoneOffset ofOffset() method in Java with Examples

The ofOffset() method of the ZoneOffset class used to obtain an instance of ZoneOffset wrapping an offset.If the prefix passed in this method is “GMT”, “UTC”, or “UT” a ZoneOffset with the prefix then the non-zero offset is returned and If the prefix is empty “” then the ZoneOffset is returned.

Syntax:

ublic static ZoneId ofOffset(String prefix,
                              ZoneOffset offset)

Parameters: This method accepts two parameters prefix and offset where prefix represents the time-zone ID and offset represents the offset.



Return value: This method returns the zoneId.

Exception: This method throws an IllegalArgumentException if the prefix is not one of “GMT”, “UTC”, or “UT”, or “”.

Below programs illustrate the ofOffset() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// ZoneOffset.ofOffset() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create ZoneId object
        ZoneId zoneId
            = ZoneId.ofOffset("UTC",
                              ZoneOffset.UTC);
  
        // Print the ZoneOffset
        System.out.println("ZoneOffset: "
                           + zoneId);
    }
}

chevron_right


Output:

ZoneOffset: UTC

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// ZoneOffset.ofOffset() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create ZoneId object
        ZoneId zoneId
            = ZoneId.ofOffset("GMT",
                              ZoneOffset.MAX);
        System.out.println("ZoneOffset: "
                           + zoneId);
    }
}

chevron_right


Output:

ZoneOffset: GMT+18:00

Reference: Oracle Doc



My Personal Notes arrow_drop_up

Recommended Posts: