Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert milliseconds into days?

Tags:

java

I need number of days from milliseconds.

I am doing as,

long days = (millis / (60*60*24*1000)) % 365;

Is this true? If no please tell me how get number of days from milliseconds.

Please don't suggest to do

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
like image 260
Apurva Avatar asked Mar 01 '15 19:03

Apurva


3 Answers

int days = TimeUnit.MILLISECONDS.toDays(miliseconds);
like image 150
Trynkiewicz Mariusz Avatar answered Oct 11 '22 00:10

Trynkiewicz Mariusz


long days = (millis / (60*60*24*1000))
like image 36
Gabriele Mariotti Avatar answered Oct 11 '22 01:10

Gabriele Mariotti


Pretty sure that's correct, but without the modulo.

% 365 means divide it by 365 and get the remainder.

There are (60*60*24*1000) millisecond in a day.

So for conversion:

millis/(60 seconds * 60 minutes * 24 hours * 1000 ms/second)

should do it.

like image 25
David Crosby Avatar answered Oct 11 '22 00:10

David Crosby