Home Java Java-conversion Convert Time to Seconds



Convert Time to Seconds
Posted on: August 9, 2007 at 12:00 AM
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/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();
  System.out.println(ts.secondsToString(time));
  }
}

Output of the program:

C:\unique>javac TimeToStr.java

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

C:\unique>

Download this example.

Related Tags for Convert Time to Seconds:
cstringormantformtimeconstructorconvertstructhelpsedconstdisplaychangethisshowforexampletoexamsseldsheitlshangspluseulimfirstinrmpassconvertingasmntplaypstrwanminosminosispesallmehowminutetorxaxampsspsoctorathourisirhaimellmplandconsstrrtsecondswantssriringthshocondavbelosthatctodispetcetclsopleplmindono


More Tutorials from this section

Ask Questions?    Discuss: Convert Time to Seconds   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.