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;
public long toHours()
The method has no parameters.
This method returns the number of hours in the duration.
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());}}