Java - Write code to generate readable Time string from time in milliseconds

Requirements

Write code to generate readable Time string from time in milliseconds

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        long time = 42;
        System.out.println(generateTime(time));
    }/*from   w ww. j  av  a  2  s  .  co  m*/

    public static String generateTime(long time) {
        int totalSeconds = (int) (time / 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