java – How to correctly convert seconds to HH: mm: ss format?

Question:

I get the duration of a video from VK in seconds. I just can't translate them into HH: mm: ss format

Here is my method

public String secToStr(int duration){
    SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss");
    String time = df.format(new Date(duration));
    return time;
}

Any duration translates to 3 hours. What am I doing wrong? How can you do this in two or three lines?

Answer:

Understood. This is how it should be

public String secToStr(int duration){
    return String.format("%02d:%02d:%02d", duration / 3600, duration / 60 % 60, duration % 60);
}
Scroll to Top