The ofHours(int) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offset in hours passed as the parameter. This method takes the hours as parameter in the form of int and converts it into the ZoneOffset. The maximum supported range is from +18:00 to -18:00 inclusive.
public static ZoneOffset ofHours(int hours)
Parameters: This method accepts a parameter hours which is int to be converted into an ZoneOffset instance.
Return Value: This method returns a ZoneOffset instance parsed from the specified hours.
Exception: This method throws DateTimeException if the hours is invalid.
Below examples illustrate the ZoneOffset.ofHours() method:
Example 2: To demonstrate DateTimeException
java.time.DateTimeException: Zone offset hours not in valid range: value 20 is not in the range -18 to 18
Reference: Oracle Doc
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.