Convert Time to Milliseconds

Ads
 

Convert Time to Milliseconds

In this section, you will learn to convert Time to seconds. An hour has 3600 seconds, a minute has sixty seconds and a millisecond is one thousand part of a second i.e. an unit for measuring the time.

In this section, you will learn to convert Time to seconds. An hour has 3600 seconds, a minute has sixty seconds and a millisecond is one thousand part of a second i.e. an unit for measuring the time.

Convert Time to Milliseconds

     

In this section, you will learn to convert Time to seconds. An hour has 3600 seconds, a minute has sixty seconds and a millisecond is one thousand part of a second i.e. an unit for measuring the time. 

Description of program:

This example helps you in converting a Time to milliseconds. Here, first of all we have changed Hours to minutes, minutes to seconds and seconds to milliseconds 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 TimeToMilli{
  
  public String millisecondsToString(long time){
  int milliseconds = (int)(time % 1000);
  int seconds = (int)((time/100060);
  int minutes = (int)((time/6000060);
  int hours = (int)((time/360000024);
  String millisecondsStr = (milliseconds<10 "00" (milliseconds<100 "0" ""))+ milliseconds;
  String secondsStr = (seconds<10 "0" "")+ seconds;
  String minutesStr = (minutes<10 "0" "")+ minutes;
  String hoursStr = (hours<10 "0" "")+ hours;
  return new String(hoursStr + ":" + minutesStr + ":" + secondsStr + "." + millisecondsStr);
  }

  public static void main(String[] args){
  long time = 200000000;
  TimeToStr ts = new TimeToStr();
  System.out.println(ts.millisecondsToString(time));
  }
}

Output of the program:

C:\unique>javac TimeToMilli.java

C:\unique>java TimeToMilli
07:33:20.000

C:\unique>

Download this example.

Ads

Ads