Java Convert Minutes to Readable Time String

Tags: TimeUnit

In this Java tutorial we learn how to convert a number of minutes value into readable time string in format of days, hours and minutes value in Java programming language.

How to convert number of minute to readable time string

At first step, we create a new Java class named TimeUtil and implement the method named getReadableTimeFromMinutes(long numberOfMinutes) to convert a number of minutes to readable time String such as “2 days 3 hours 4 minutes” as the following Java code.

TimeUtil.java

import java.util.concurrent.TimeUnit;

public class TimeUtil {

    /**
     * Convert number of minutes value to readable time string
     * Example of readable time String:
     * - 2 days 3 hours 1 minute
     * - 1 day 1 hour 50 minutes
     * - 10 hours 1 minute
     * @param numberOfMinutes number of minutes
     * @return readable time string
     */
    public static String getReadableTimeFromMinutes(long numberOfMinutes) {
        long numberOfDays = TimeUnit.MINUTES.toDays(numberOfMinutes);
        numberOfMinutes -= TimeUnit.DAYS.toMinutes(numberOfDays);

        long numberOfHours = TimeUnit.MINUTES.toHours(numberOfMinutes);
        numberOfMinutes -= TimeUnit.HOURS.toMinutes(numberOfHours);

        StringBuilder stringBuilder = new StringBuilder();

        if(numberOfDays > 0) {
            if(numberOfDays == 1)
                stringBuilder.append(String.format("%d day ", numberOfDays));
            else
                stringBuilder.append(String.format("%d days ", numberOfDays));
        }

        if(numberOfHours > 0) {
            if(numberOfHours == 1)
                stringBuilder.append(String.format("%d hour ", numberOfHours));
            else
                stringBuilder.append(String.format("%d hours ", numberOfHours));
        }

        if(numberOfMinutes > 0) {
            if(numberOfMinutes == 1)
                stringBuilder.append(String.format("%d minute ", numberOfMinutes));
            else
                stringBuilder.append(String.format("%d minutes ", numberOfMinutes));
        }

        return stringBuilder.toString();
    }
}

In the following example Java program, we learn how to use the TimeUtil class above to convert specified number of minutes to a readable time String.

TimeUtilExample1.java

public class TimeUtilExample1 {
    public static void main(String... args) {
        String string1 = TimeUtil.getReadableTimeFromMinutes(3000);
        String string2 = TimeUtil.getReadableTimeFromMinutes(2000);
        String string3 = TimeUtil.getReadableTimeFromMinutes(125);
        String string4 = TimeUtil.getReadableTimeFromMinutes(40);

        System.out.println(string1);
        System.out.println(string2);
        System.out.println(string3);
        System.out.println(string4);
    }
}
The output as below.
2 days 2 hours 
1 day 9 hours 20 minutes 
2 hours 5 minutes 
40 minutes

Happy Coding 😊

Java Convert Milliseconds to Readable Time String

Java Convert Seconds to Readable Time String

Java Convert Hours to Readable Time String