Java Convert Number of Hours to Days

Tags: TimeUtil

In this Java tutorial, we learn how to convert a number of hours to number of days in Java programming language.

How to convert number of hours to days in Java

In the first step, we create the TimeUtil class the hoursToDays(int numberOfHours) method to get number of hours in integer and return number of days in double data type as below.

TimeUtil.java

public class TimeUtil {

    /**
     * Convert number of hours to number of days
     * @param numberOfHours number of hours
     * @return number of days
     */
    public static double hoursToDays(int numberOfHours) {
        double numberOfDays = Math.rint(numberOfHours / 24.0 * 100.0) / 100.0;
        return numberOfDays;
    }
}

In the following Java program, we show how to use the above TimeUtil class to convert number of hours values into number of days values.

ConvertHoursToDays.java

public class ConvertHoursToDays {
    public static void main(String... args) {
        int numberOfHours = 36;

        // Convert Number of Hours to Days
        double numberOfDays = TimeUtil.hoursToDays(numberOfHours);

        System.out.println("Number of hours: " + numberOfHours);
        System.out.println("Number of days: " + numberOfDays);
    }
}
The output as below.
Number of hours: 36
Number of days: 1.5

Happy Coding 😊

Java Convert Number of Minutes to Hours

Java Convert Number of Seconds to Minutes

Java Convert TimeUnit to ChronoUnit

Java Convert ChronoUnit to TimeUnit