user523956 user523956 - 5 days ago 4x
Java Question

Get time in hh:mm:ss from seconds in Java

I want to convert seconds into

time format, so:

seconds = 3754
result = 10:25:40

I know about the conventional approach of dividing it by 3600 to get hours an so on, but was wondering if I can achieve this through Java API?


Using java.util.Calendar:

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.MILLISECOND, 0);      
    calendar.set(Calendar.SECOND, 37540);
    System.out.println(new SimpleDateFormat("HH:mm:ss").format(calendar.getTime()));