Duration ofHours(long) method in Java with Examples Last Updated : 13 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report The ofHours(long) method of Duration Class in java.time package is used to get a duration in a 1 hour format. In this method, the seconds are calculated as total seconds in 1 hour format, i.e. 3600 seconds per hour. Syntax: public static Duration ofHours(long hours) Parameters: This method accepts a parameter hours which is the number of hours. It can be positive or negative. Return Value: This method returns a Duration representing the time in 1 hour format. Exception: This method throws ArithmeticException if the input hours exceeds the capacity of Duration. Below examples illustrate the Duration.ofHours() method: Example 1: Java // Java code to illustrate ofHours(long) method import java.time.Duration; public class GFG { public static void main(String[] args) { // input number of Hours long noOfHours = 5; // Duration using ofHours() method Duration duration = Duration.ofHours(noOfHours); System.out.println(duration.getSeconds()); } } Output:18000 Example 2: Java // Java code to illustrate ofHours(long) method import java.time.Duration; public class GFG { public static void main(String[] args) { // input number of Hours long noOfHours = -214545; // Duration using ofHours() method Duration duration = Duration.ofHours(noOfHours); System.out.println(duration.getSeconds()); } } Output:-772362000 Reference: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#ofHours-long- Comment More infoAdvertise with us Next Article Duration toHours() method in Java with Examples C code_r Follow Improve Article Tags : Java Java-Functions Java-time package Java-Duration Practice Tags : Java Similar Reads Duration plusHours(long) method in Java with Examples The plusHours(long) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified number of hours added, passed as the parameter. Syntax: public Duration plusHours(long numberOfHours) Parameters: This method accepts a parameter numberOfHours which 1 min read Duration ofNanos(long) method in Java with Examples The ofNanos(long) method of Duration Class in java.time package is used to get a duration in a 1 nanoSecond format. Syntax: public static Duration ofNanos(long nanoSeconds) Parameters: This method accepts a parameter nanoSeconds which is the number of nanoSeconds. It can be positive or negative. Ret 1 min read Duration ofDays(long) method in Java with Examples The ofDays(long) method of Duration Class in java.time package is used to get a duration in a 24 hour format. In this method, the seconds are calculated as total seconds in 24 hour day format, i.e. 86400 seconds per day. Syntax: public static Duration ofDays(long days) Parameters: This method accept 1 min read Duration minusHours(long) method in Java with Examples The minusHours(long) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified number of hours subtracted, passed as the parameter.Syntax: public Duration minusHours(long numberOfHours) Parameters: This method accepts a parameter numberOfHours 1 min read Duration toHours() method in Java with Examples The toHours() method of Duration Class in java.time package is used get the value of this duration in number of hours. Syntax: public long toHours() Parameters: This method do not accepts any parameter. Return Value: This method returns a long value which is the number of hours in this duration. Bel 1 min read Duration ofMinutes(long) method in Java with Examples The ofMinutes(long) method of Duration Class in java.time package is used to get a duration in a 1 minute format. In this method, the seconds are calculated as total seconds in 1 minute format, i.e. 60 seconds per minute. Syntax: public static Duration ofMinutes(long minutes) Parameters: This method 1 min read Like