Search Suggest

Java Convert Seconds to Time (HH:mm:ss)



How to convert seconds into HH:mm:ss time format ?
seconds = 3754 
result
= 01:02:34

    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, 3754);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(calendar.getTime()));

Output: 01:02:34

Or

The java.time classes built into Java can do this. The LocalTime class represents a time-of-day without a date and without a time zone. This class includes the concept of second-of-day, how many seconds from the start of the day.
LocalTime lt = LocalTime.ofSecondOfDay(3754);
But the result is not close to what you showed in your Question.
lt.toString(): 01:02:34

Reference :

Đăng nhận xét