Open In App

Duration ofHours(long) method in Java with Examples

Last Updated : 13 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
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-



Similar Reads

ZoneOffset ofHours(int) method in Java with Examples
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. Syntax: public static ZoneOff
2 min read
Duration ofSeconds(long, long) method in Java with Examples
The ofSeconds(long, long) method of Duration Class in java.time package is used to get a duration in a 1 second format. In this method, the seconds are calculated as total seconds in 1 second format, i.e. 1 second per second. The second parameter nanoAdjustment is used to adjust the seconds in nanoSeconds unit. Syntax: public static Duration ofSeco
2 min read
Duration plus(Duration) method in Java with Examples
The plus(Duration) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified duration added, passed as the parameter. Syntax: public Duration plus(Duration duration) Parameters: This method accepts a parameter duration which is the duration to be added. It can be positive or negative but not
2 min read
Duration equals(Duration) method in Java with Examples
The equals(Duration) method of Duration Class in java.time package is used to check whether this duration is equal to the duration passed as the parameter. Syntax: public boolean equals(Duration otherDuration) Parameters: This method accepts a parameter otherDuration which is the duration to which this duration will be compared to. Return Value: Th
2 min read
Duration compareTo(Duration) method in Java with Examples
The compareTo(Duration) method of Duration Class in java.time package is used to compare this duration with the duration passed as the parameter. Syntax: public int compareTo(Duration otherDuration) Parameters: This method accepts a parameter otherDuration which is the duration to which this duration will be compared to. Return Value: This method r
2 min read
Duration minus(Duration) method in Java with Examples
The minus(Duration) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified duration subtracted, passed as the parameter.Syntax: public Duration minus(Duration duration) Parameters: This method accepts a parameter duration which is the duration to be subtracted. It can be positive or negati
2 min read
Duration dividedBy(Duration) method in Java with Examples
The dividedBy(Duration) method of Duration Class in java.time package is used to get an immutable copy of this duration divided by the duration passed as the parameter. Syntax: public Duration dividedBy(Duration divisor) Parameters: This method accepts a parameter divisor which is the duration to be divided. It can be positive or negative duration.
2 min read
Duration ofSeconds(long) method in Java with Examples
The ofSeconds(long) method of Duration Class in java.time package is used to get a duration in a 1 second format. In this method, the seconds are calculated as total seconds in 1 second format, i.e. 1 second per second. Syntax: public static Duration ofSeconds(long seconds) Parameters: This method accepts a parameter seconds which is the number of
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 accepts a parameter days which is the number of days. It
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 accepts a parameter minutes which is the number o
1 min read
Practice Tags :