ZoneOffset ofHoursMinutesSeconds(int, int, int) method in Java with Examples
The ofHoursMinutesSeconds(int, int, int) method of ZoneOffset Class in java.time package is used to obtain an instance of ZoneOffset using the offset in hours, minutes and seconds passed as the parameter. This method takes the hours, minutes and seconds as parameter in the form of int and converts it into the ZoneOffset.
Syntax:
public static ZoneOffset
ofHoursMinutesSeconds(int hours,
int minutes,
int seconds)
Parameters: This method accepts 3 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
- seconds: which is int seconds 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, minutes and seconds.
Exception: This method throws DateTimeException if the hours, minutes and seconds is invalid.
Below examples illustrate the ZoneOffset.ofHoursMinutesSeconds() method:
Example 1:
// Java code to illustrate ofHoursMinutesSeconds() method import java.time.*; public class GFG { public static void main(String[] args) { // Get the hours, minutes and seconds int hours = 5; int minutes = 20; int seconds = 50; // ZoneOffset using ofHoursMinutesSeconds() method ZoneOffset zoneOffset = ZoneOffset .ofHoursMinutesSeconds(hours, minutes, seconds); System.out.println(zoneOffset); } } |
+05:20:50
Example 2: To demonstrate DateTimeException
// Java code to illustrate ofHoursMinutesSeconds() method import java.time.*; public class GFG { public static void main(String[] args) { // Get the invalid hours, minutes and seconds int hours = 20; int minutes = 5; int seconds = 0; try { // ZoneOffset using ofHoursMinutesSeconds() method ZoneOffset zoneOffset = ZoneOffset .ofHoursMinutesSeconds(hours, minutes, seconds); } catch (Exception e) { System.out.println(e); } } } |
java.time.DateTimeException: Zone offset hours not in valid range: value 20 is not in the range -18 to 18
Reference: Oracle Doc
Recommended Posts:
- ZoneOffset compareTo(ZoneOffset) method in Java with Examples
- ZoneOffset of(String) method in Java with Examples
- ZoneOffset ofTotalSeconds(int) method in Java with Examples
- ZoneOffset getId() method in Java with Examples
- ZoneOffset toString() method in Java with Examples
- ZoneOffset hashCode() method in Java with Examples
- ZoneOffset ofHours(int) method in Java with Examples
- ZoneOffset ofOffset() method in Java with Examples
- ZoneOffset systemDefault() method in Java with Examples
- ZoneOffset normalized() method in Java with Examples
- ZoneOffset getDisplayName() method in Java with Examples
- ZoneOffset from(TemporalAccessor) method in Java with Examples
- ZoneOffset get(TemporalField) method in Java with Examples
- ZoneOffset ofHoursMinutes(int, int) method in Java with Examples
- ZoneOffset getRules() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


