Java Convert Number of Hours to Seconds

Tags: Java Duration Java 8

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

How to convert number of hours to seconds in Java

In Java, with a given number of hours we can use the Duration.toSeconds() method to convert it to number of seconds as the following Java program.

ConvertHoursToSecondsExample.java

import java.time.Duration;

public class ConvertHoursToSecondsExample {
    public static void main(String... args) {
        long numberOfHours = 2;

        // Convert Number of Hours to Seconds
        long numberOfSeconds = Duration.ofHours(numberOfHours).toSeconds();

        System.out.println("Number of hours: " + numberOfHours);
        System.out.println("Number of seconds: " + numberOfSeconds);
    }
}
The output as below.
Number of hours: 2
Number of seconds: 7200

Happy Coding 😊

Java Convert Number of Days to Nanoseconds

Java Convert Number of Hours to Minutes

Java Convert Number of Hours to Milliseconds

Java Convert Number of Hours to Nanoseconds

Java Convert Number of Minutes to Seconds

Java Convert Number of Minutes to Milliseconds

Java Convert Number of Minutes to Nanoseconds

Java Convert Number of Seconds to Milliseconds

Java Convert Number of Seconds to Nanoseconds

Java Convert Number of Milliseconds to Nanoseconds