Java Convert Number of Minutes to Hours
Tags: TimeUtil
In this Java tutorial, we learn how to convert a number of minutes into number of hours in Java programming language.
How to convert number of minutes to hours in Java
In the first step, we create a new Java utility class named TimeUtil, and implement a new method minutesToHours(int numberOfMinutes) which get number of minutes as an integer value and return number of hours as a double data type value.
TimeUtil.java
public class TimeUtil {
/**
* Convert number of minutes to number of hours
* @param numberOfMinutes number of minutes
* @return number of hours
*/
public static double minutesToHours(int numberOfMinutes) {
double numberOfHours = Math.rint(numberOfMinutes / 60.0 * 100.0) / 100.0;
return numberOfHours;
}
}
In the following Java program, we show how to use the above TimeUtil class to convert a number of minutes to hours.
ConvertMinutesToHours.java
public class ConvertMinutesToHours {
public static void main(String... args) {
int numberOfMinutes = 90;
// Convert Number of Minutes to Hours
double numberOfHours = TimeUtil.minutesToHours(numberOfMinutes);
System.out.println("Number of minutes: " + numberOfMinutes);
System.out.println("Number of hours: " + numberOfHours);
}
}
Number of minutes: 90
Number of hours: 1.5
Happy Coding 😊
Related Articles
Java Convert Number of Hours to Days
Java Convert Number of Seconds to Minutes