Java Convert Milliseconds to Readable Time String

Tags: TimeUnit

In this Java tutorial, we learn how to convert number of milliseconds to a readable time string in format of number of days, hours, minutes, seconds and milliseconds.

How to convert number of milliseconds to readable time string

Firstly, we implement a new Java class named TimeUtil with a method named getReadableTime(long numberOfMilliseconds) to convert a number of milliseconds to a readable time String value such as “2 days 3 hours 4 minutes 5 seconds 100 milliseconds” as Java code below.

TimeUtil.java

import java.util.concurrent.TimeUnit;

public class TimeUtil {

    /**
     * Convert a number of milliseconds to a readable String
     * Example readable time String
     * - 2 days 3 hours 4 minutes 5 seconds 100 milliseconds
     * - 1 day 1 hour 1 minute 5 seconds 100 milliseconds
     * - 20 minutes 50 seconds 500 milliseconds
     * @param numberOfMilliseconds number of milliseconds
     * @return readable String from number of milliseconds
     */
    public static String getReadableTime(long numberOfMilliseconds) {
        long numberOfDays = TimeUnit.MILLISECONDS.toDays(numberOfMilliseconds);
        numberOfMilliseconds -= TimeUnit.DAYS.toMillis(numberOfDays);

        long numberOfHours = TimeUnit.MILLISECONDS.toHours(numberOfMilliseconds);
        numberOfMilliseconds -= TimeUnit.HOURS.toMillis(numberOfHours);

        long numberOfMinutes = TimeUnit.MILLISECONDS.toMinutes(numberOfMilliseconds);
        numberOfMilliseconds -= TimeUnit.MINUTES.toMillis(numberOfMinutes);

        long numberOfSeconds = TimeUnit.MILLISECONDS.toSeconds(numberOfMilliseconds);
        numberOfMilliseconds -= TimeUnit.SECONDS.toMillis(numberOfSeconds);

        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));
        }

        if(numberOfSeconds > 0) {
            if(numberOfSeconds == 1)
                stringBuilder.append(String.format("%d second ", numberOfSeconds));
            else
                stringBuilder.append(String.format("%d seconds ", numberOfSeconds));
        }

        if(numberOfMilliseconds > 0) {
            if(numberOfMilliseconds == 1)
                stringBuilder.append(String.format("%d millisecond", numberOfMilliseconds));
            else
                stringBuilder.append(String.format("%d milliseconds", numberOfMilliseconds));
        }

        return stringBuilder.toString();
    }
}

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

TimeUtilExample1.java

public class TimeUtilExample1 {
    public static void main(String... args) {
        String string1 = TimeUtil.getReadableTime(100000000);
        String string2 = TimeUtil.getReadableTime(200000500);
        String string3 = TimeUtil.getReadableTime(9004500);
        String string4 = TimeUtil.getReadableTime(700000);

        System.out.println(string1);
        System.out.println(string2);
        System.out.println(string3);
        System.out.println(string4);
    }
}
The output as below.
1 day 3 hours 46 minutes 40 seconds 
2 days 7 hours 33 minutes 20 seconds 500 milliseconds
2 hours 30 minutes 4 seconds 500 milliseconds
11 minutes 40 seconds

Happy Coding 😊

Java Convert Seconds to Readable Time String

Java Convert Minutes to Readable Time String

Java Convert Hours to Readable Time String