Java : How to convert seconds to Time?

In this blog, you will learn to convert seconds to time. An hour has 60*60=3600 seconds and a minute has 60 seconds.

Example
In this example helps you to convert total seconds to time as (hour, minute an d second). To show as text String convert these these values to String.

public class ConvertSecondToTime {

public static void main(String[] args) {
 long time = 180500700;
 ConvertSecondToTime ts = new ConvertSecondToTime();
 System.out.println("Inserted Total Seconds :"+time);
 System.out.println(ts.secondsToTimeString(time));
}

public static String secondsToTimeString(long time) {
 int seconds = (int) (time % 60);
 int minutes = (int) ((time / 60) % 60);
 int hours = (int) ((time / 3600) % 24);
 String secondsTxt = (seconds < 10 ? "0" : "") + seconds;
 String minutesTxt = (minutes < 10 ? "0" : "") + minutes;
 String hoursTxt = (hours < 10 ? "0" : "") + hours;
 return new String("Converted Time :"+ hoursTxt + " Hour : " + minutesTxt + " Minute :" + secondsTxt + " Second");
 }
}

Output


Inserted Total Seconds :180500700
Converted Time :03 Hour : 05 Minute :00 Second