# 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.

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.

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>

Online Training Classes

# Convert Time to Seconds

Related Tutorials

Discuss: Convert Time to Seconds   View All Comments

Subject (*):

Julius Caesar G. Leonardo
January 6, 2012
my sample

//second to Hour to minute to second // W/ s or w/out S import java.util.*; import java.io.*; import static java.lang.System.out; class sec { public static void main (String[] args)throws IOException { Runner(); } public static void Runner()throws IOException { Scanner scan = new Scanner(System.in); out.println("Enter Second"); int secsIn = scan.nextInt(); if(secsIn==0) { System.exit(0); }else{ int h = secsIn/3600; int remainder = secsIn % 3600; int minutes = remainder / 60; int seconds = remainder % 60; if(h<=1 && minutes<=1 && seconds<=1) { System.out.println(h+"hour:" + minutes+"minute:" +seconds+"Second"); }else if(h<=1 && minutes<=1 && seconds>=2) { System.out.println(h+"hour:" + minutes+"minute:" +seconds+"Seconds"); }else if(h<=1 && minutes>=2 && seconds<=1) { System.out.println(h+"hour:" + minutes+"minutes:" +seconds+"Second"); }else if(h<=1 && minutes>=2 && seconds>=2) { System.out.println(h+"hour:" + minutes+"minutes:" +seconds+"Seconds"); } else if(h>=2 && minutes<=1 && seconds<=1) { System.out.println(h+"hours:" + minutes+"minute:" +seconds+"Second"); }else if(h>=2 && minutes<=1 && seconds>=2) { System.out.println(h+"hours:" + minutes+"minute:" +seconds+"Seconds"); }else if(h>=2 && minutes>=2 && seconds<=1) { System.out.println(h+"hours:" + minutes+"minutes:" +seconds+"Second"); }else if(h>=2 && minutes>=2 && seconds>=2) { System.out.println(h+" hours " + minutes+" minutes " +seconds+" Seconds "); } Runner(); }}}