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