Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
tohours
duration

What is Duration.toHours() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 days
System.out.println("10 days in hours - " + durationInDays.toHours());
Duration durationInMinutes = Duration.ofMinutes(100); // Get number of seconds in 100 minutes
System.out.println("100 minutes in hours - " + durationInMinutes.toHours());
}
}

RELATED TAGS

java
communitycreator
tohours
duration

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring