Java Convert Hours to Duration

Tags: Java Duration Java 8

In this Java core tutorial we learn how to convert a number of hours to java.time.Duration object in Java programming language.

How to convert hours to Duration in Java

In Java, we can use the Duration.ofHours(long hours) static method to instantiate a Duration object from number of hours as the example Java code below.

ConvertHoursToDurationExample1.java

import java.time.Duration;

public class ConvertHoursToDurationExample1 {
    public static void main(String... args) {
        long numberOfHours = 789;

        // Convert number of hours to Duration
        Duration duration = Duration.ofHours(numberOfHours);

        System.out.println("Number of hours: " + numberOfHours);
        System.out.println("Duration: " + duration);
    }
}
The output as below.
Number of hours: 789
Duration: PT789H

Happy Coding 😊

Java Convert Days to Duration

Java Convert Minutes to Duration

Java Convert Seconds to Duration

Java Convert Milliseconds to Duration

Java Convert Nanoseconds to Duration

Java Convert String to Duration

Java Convert Duration to Seconds