Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from days to milliseconds

I want to create a function that will convert the days into milliseconds. The days format is stored as 0.2444, so how to convert this to milliseonds?

like image 468
maas Avatar asked Aug 08 '11 09:08

maas


People also ask

How do you convert days to milliseconds?

To convert days to milliseconds, multiply the days by 24 for the hours, 60 for the minutes, 60 for the seconds and 1000 for the milliseconds, e.g. days * 24 * 60 * 60 * 1000 . Copied!

How much is a 1 millisecond?

A millisecond (from milli- and second; symbol: ms) is one thousandth (0.001 or 10−3 or 1/1000) of a second.

How do you calculate milliseconds?

To convert a second measurement to a millisecond measurement, multiply the time by the conversion ratio. The time in milliseconds is equal to the seconds multiplied by 1,000.

What is milliseconds in time?

A millisecond (ms or msec) is one thousandth of a second and is commonly used in measuring the time to read to or write from a hard disk or a CD-ROM player or to measure packet travel time on the Internet. For comparison, a microsecond (us or Greek letter mu plus s) is one millionth (10-6) of a second.


2 Answers

The best practice for this, in my opinion is:

TimeUnit.DAYS.toMillis(1);     // 1 day to milliseconds. TimeUnit.MINUTES.toMillis(23); // 23 minutes to milliseconds. TimeUnit.HOURS.toMillis(4);    // 4 hours to milliseconds. TimeUnit.SECONDS.toMillis(96); // 96 seconds to milliseconds. 
like image 153
E_X Avatar answered Nov 10 '22 14:11

E_X


In addition to the other answers, there is also the TimeUnit class which allows you to convert one time duration to another. For example, to find out how many milliseconds make up one day:

TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS); //gives 86400000 

Note that this method takes a long, so if you have a fraction of a day, you will have to multiply it by the number of milliseconds in one day.

like image 20
dogbane Avatar answered Nov 10 '22 14:11

dogbane