Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

milliseconds to days

i did some research, but still can't find how to get the days... Here is what I got:

int seconds = (int) (milliseconds / 1000) % 60 ; int minutes = (int) ((milliseconds / (1000*60)) % 60); int hours   = (int) ((milliseconds / (1000*60*60)) % 24); int days = ????? ; 

Please help, I suck at math, thank's.

like image 276
Reacen Avatar asked Oct 19 '11 23:10

Reacen


People also ask

How do you convert milliseconds to days?

How to Convert Milliseconds to Days. To convert a millisecond measurement to a day measurement, divide the time by the conversion ratio. The time in days is equal to the milliseconds divided by 86,400,000.

How long is a 1 millisecond?

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.

How much is a 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 convert milliseconds to days in groovy?

int days = (int) (milliseconds / (1000*60*60*24));


2 Answers

For simple cases like this, TimeUnit should be used. TimeUnit usage is a bit more explicit about what is being represented and is also much easier to read and write when compared to doing all of the arithmetic calculations explicitly. For example, to calculate the number days from milliseconds, the following statement would work:

    long days = TimeUnit.MILLISECONDS.toDays(milliseconds); 

For cases more advanced, where more finely grained durations need to be represented in the context of working with time, an all encompassing and modern date/time API should be used. For JDK8+, java.time is now included (here are the tutorials and javadocs). For earlier versions of Java joda-time is a solid alternative.

like image 193
whaley Avatar answered Oct 16 '22 06:10

whaley


If you don't have another time interval bigger than days:

int days = (int) (milliseconds / (1000*60*60*24)); 

If you have weeks too:

int days = (int) ((milliseconds / (1000*60*60*24)) % 7); int weeks = (int) (milliseconds / (1000*60*60*24*7)); 

It's probably best to avoid using months and years if possible, as they don't have a well-defined fixed length. Strictly speaking neither do days: daylight saving means that days can have a length that is not 24 hours.

like image 23
Mark Byers Avatar answered Oct 16 '22 07:10

Mark Byers