What is Duration.toHours() in Java?
toHours() is an instance method of the Duration class, used to get the number of hours in the duration object. This method returns the total number of hours in the duration by dividing the number of seconds by 3600.
The toHours() method is defined in the Duration class. The Duration class is defined in the java.time package. To import the Duration class, check the following import statement:
import java.time.Duration;
Syntax
public long toHours()
Parameters
The method has no parameters.
Return value
This method returns the number of hours in the duration.
Code
In the below code, Duration class objects of different time units are converted to hours.
import java.time.Duration;public class Main{public static void main(String[] args) {Duration durationInDays = Duration.ofDays(10); // Get number of seconds in 10 daysSystem.out.println("10 days in hours - " + durationInDays.toHours());Duration durationInMinutes = Duration.ofMinutes(100); // Get number of seconds in 100 minutesSystem.out.println("100 minutes in hours - " + durationInMinutes.toHours());}}