Java long seconds value convert to time as "00:00:00" via String.format()

Description

Java long seconds value convert to time as "00:00:00" via String.format()

public class Main {
    public static void main(String[] argv) {
        int timeMs = 12312342;
        System.out.println(stringForTime(timeMs));
    }/*from  ww  w.  j  a  va  2  s  .  com*/

    public static String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes,
                seconds) : String.format("%02d:%02d", minutes, seconds);
    }
}



PreviousNext

Related