Tutorials

# Convert Time to Seconds

In this section, you will learn to convert Time to seconds. An hour has 3600 seconds and a minute has sixty seconds.

In this section, you will learn to convert Time to seconds. An hour has 3600 seconds and a minute has sixty seconds.

# Convert Time to Seconds

In this section, you will learn to convert Time to seconds. An hour has 3600 seconds and a minute has sixty seconds.

Description of program:

This example helps you in converting a Time to seconds. Here, first of all we have changed Hours to minutes, minutes to seconds etc as shown below. We also want that the Time would be displayed in the form of String so we have used a TimetoStr constructor here and we have passed time to it. Therefore we get the following output.

 ``` import java.util.*; import java.io.*; public class TimeToStr{   public static String secondsToString(long time){   int seconds = (int)(time % 60);   int minutes = (int)((time/60) % 60);   int hours = (int)((time/3600) % 24);   String secondsStr = (seconds<10 ? "0" : "")+ seconds;   String minutesStr = (minutes<10 ? "0" : "")+ minutes;   String hoursStr = (hours<10 ? "0" : "")+ hours;   return new String(hoursStr + ":" + minutesStr + ":" + secondsStr);   }``` ```  public static void main(String[] args){   long time = 200000000;   TimeToStr ts = new TimeToStr();   System.out.println(ts.secondsToString(time));   } }```

Output of the program:

 C:\unique>javac TimeToStr.java C:\unique>java TimeToStr 19:33:20 C:\unique>