Java Convert Seconds to Readable Time String

Tags: TimeUnit

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

How to convert number of seconds to readable time string

At first step, we implement a new Java class named TimeUtil and implement a static method getReadableTimeFromSeconds(long numberOfSeconds) to convert number of seconds value to readable time string such as “2 days 3 hours 4 minutes 5 seconds” as the following Java code.

TimeUtil.java

import java.util.concurrent.TimeUnit;

public class TimeUtil {

    /**
     * Convert number of seconds into a readable time string
     * Example readable time String
     * - 2 days 3 hours 1 minute 1 second
     * - 1 day 1 hour 4 minutes 5 seconds
     * - 10 hours 45 minutes 50 seconds
     * @param numberOfSeconds number of seconds
     * @return readable time string
     */
    public static String getReadableTimeFromSeconds(long numberOfSeconds) {
        long numberOfDays = TimeUnit.SECONDS.toDays(numberOfSeconds);
        numberOfSeconds -= TimeUnit.DAYS.toSeconds(numberOfDays);

        long numberOfHours = TimeUnit.SECONDS.toHours(numberOfSeconds);
        numberOfSeconds -= TimeUnit.HOURS.toSeconds(numberOfHours);

        long numberOfMinutes = TimeUnit.SECONDS.toMinutes(numberOfSeconds);
        numberOfSeconds -= TimeUnit.MINUTES.toSeconds(numberOfMinutes);

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

        return stringBuilder.toString();
    }
}

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

TimeUtilExample1.java

public class TimeUtilExample1 {
    public static void main(String... args) {
        String string1 = TimeUtil.getReadableTimeFromSeconds(200000);
        String string2 = TimeUtil.getReadableTimeFromSeconds(50000);
        String string3 = TimeUtil.getReadableTimeFromSeconds(600);
        String string4 = TimeUtil.getReadableTimeFromSeconds(200);

        System.out.println(string1);
        System.out.println(string2);
        System.out.println(string3);
        System.out.println(string4);
    }
}
The output as below.
2 days 7 hours 33 minutes 20 seconds 
13 hours 53 minutes 20 seconds 
10 minutes 
3 minutes 20 seconds

Happy Coding 😊

Java Convert Milliseconds to Readable Time String

Java Convert Minutes to Readable Time String

Java Convert Hours to Readable Time String