Saturday, October 25, 2025
HomeLanguagesJavaZoneOffset ofHoursMinutes(int, int) method in Java with Examples

ZoneOffset ofHoursMinutes(int, int) method in Java with Examples

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

public static ZoneOffset 
    ofHoursMinutes(int hours, int minutes)

Parameters: This method accepts two parameters:

  • hours: which is int hours to be converted into an ZoneOffset instance. Its range is +18 to -18
  • minutes: which is int minutes to be converted into an ZoneOffset instance. Its range is -59 to +59

Return Value: This method returns a ZoneOffset instance parsed from the specified hours and minutes. Exception: This method throws DateTimeException if the hours and minutes is invalid. Below examples illustrate the ZoneOffset.ofHoursMinutes() method: Example 1: 

Java




// Java code to illustrate ofHoursMinutes() method
 
import java.time.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Get the hours and minutes
        int hours = 5;
        int minutes = 20;
 
        // ZoneOffset using ofHoursMinutes() method
        ZoneOffset zoneOffset
            = ZoneOffset.ofHoursMinutes(hours, minutes);
 
        System.out.println(zoneOffset);
    }
}


Output:

+05:20

= Example 2: To demonstrate DateTimeException 

Java




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


Output:

java.time.DateTimeException:
 Zone offset hours not in valid range:
 value 20 is not in the range -18 to 18

Reference: Oracle Doc

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

Most Popular

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