What is Duration.ofHours() in Java?
ofHours() is a Duration class that is used to get a Duration class instance that corresponds to the number of standard hours. The seconds are determined using the standard definition of an hour, which is 3600 seconds.
Duration is defined in the java.time package. To import the Duration class, use the import statement below:
import java.time.Duration;
Syntax
public static Duration ofHours(long hours)
Parameters
long hours: The number of hours. It can be positive or negative.
Return value
The method returns a Duration object.
Code
In the code below, we create two Duration class objects with a positive and negative number of hours. Then we print the objects to the console.
import java.time.Duration;public class Main{public static void main(String[] args) {Duration positiveHours = Duration.ofHours(5);System.out.println("Positive Hours - " + positiveHours);Duration negativeHours = Duration.ofHours(-5);System.out.println("Negative Hours - " + negativeHours);}}