Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (10.2k points)

I'm confused. After stumbling upon this thread, I tried to figure out how to format a countdown timer that had the format hh:mm:ss.

Here's my attempt -




    TimeUnit.MILLISECONDS.toMinutes(millis) - 


    TimeUnit.MILLISECONDS.toSeconds(millis) - 


So, when I try a value like 3600000ms, I get 01:59:00, which is wrong since it should be 01:00:00. Obviously there's something wrong with my logic, but at the moment, I cannot see what it is!

Can anyone help?

1 Answer

0 votes
by (46k points)

The generic method for this is fairly simple:

public static String convertSecondsToHMmSs(long seconds) {

    long s = seconds % 60;

    long m = (seconds / 60) % 60;

    long h = (seconds / (60 * 60)) % 24;

    return String.format("%d:%02d:%02d", h,m,s);


Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.6k answers


108k users

Browse Categories