Share on Google+Share on Google+

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.

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.*;

public class TimeToStr{
  public static String secondsToString(long time){
  int seconds = (int)(time % 60);
  int minutes = (int)((time/6060);
  int hours = (int)((time/360024);
  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();

Output of the program:


C:\unique>java TimeToStr


Download this example.


Posted on: August 9, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Convert Time to Seconds   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
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*; 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(; 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(); }}}