Thursday, October 16, 2025
HomeLanguagesJavaZoneOffset ofTotalSeconds(int) method in Java with Examples

ZoneOffset ofTotalSeconds(int) method in Java with Examples

The ofTotalSeconds(int) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offset in totalSeconds passed as the parameter. This method takes the totalSeconds as parameter in the form of int and converts it into the ZoneOffset.

Syntax:

public static ZoneOffset
  ofTotalSeconds(int totalSeconds)

Parameters: This method accepts a parameter totalSeconds which is int to be converted into an ZoneOffset instance. ITs range is -64800 to +64800.

Return Value: This method returns a ZoneOffset instance parsed from the specified totalSeconds.

Exception: This method throws DateTimeException if the totalSeconds is invalid.

Below examples illustrate the ZoneOffset.ofTotalSeconds() method:

Example 1:




// Java code to illustrate ofTotalSeconds() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the totalSeconds
        int totalSeconds = 5000;
  
        // ZoneOffset using ofTotalSeconds() method
        ZoneOffset zoneOffset
            = ZoneOffset.ofTotalSeconds(totalSeconds);
  
        System.out.println(zoneOffset);
    }
}


Output:

+01:23:20

Example 2: To demonstrate DateTimeException




// Java code to illustrate ofTotalSeconds() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the invalid totalSeconds
        int totalSeconds = 100000;
  
        try {
            // ZoneOffset using ofTotalSeconds() method
            ZoneOffset zoneOffset
                = ZoneOffset.ofTotalSeconds(totalSeconds);
        }
  
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

java.time.DateTimeException:
 Zone offset not in valid range:
 -18:00 to +18:00

Reference: Oracle Doc

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11953 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS