Friday, September 19, 2025
HomeLanguagesJavaZoneId ofOffset() method in Java with Examples

ZoneId ofOffset() method in Java with Examples

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

public 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: 
 

Java




// Java program to demonstrate
// ZoneId.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 result
        System.out.println("ZoneId:"
                           + zoneId);
    }
}


Output: 

ZoneId:UTC

 

Program 2: 
 

Java




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


Output: 

ZoneId:GMT+18:00

 

References: 
https://docs.oracle.com/javase/10/docs/api/java/time/ZoneId.html#ofOffset(java.lang.String, java.time.ZoneOffset)
 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32301 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6666 POSTS0 COMMENTS
Nicole Veronica
11840 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7056 POSTS0 COMMENTS
Thapelo Manthata
6739 POSTS0 COMMENTS
Umr Jansen
6744 POSTS0 COMMENTS