Java - Write code to convert long value to Time in HH:mm:ss format

Requirements

Write code to convert long value to Time in HH:mm:ss format

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        int timeMs = 12312342;
        System.out.println(stringForTime(timeMs));
    }/* w ww  .  ja  va 2  s. c o  m*/

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

Related Exercise