Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show milliseconds in days:hours:min:seconds

This is what I have at the moment

Seconds = (60 - timeInMilliSeconds / 1000 % 60); Minutes = (60 - ((timeInMilliSeconds / 1000) / 60) %60); 

which I feel is correct. for hours and days should it be like -

Hours = ((((timeInMilliSeconds / 1000) / 60) / 60) % 24); Days =  ((((timeInMilliSeconds / 1000) / 60) / 60) / 24)  % 24; 

and then-

TextView.SetText("Time left:" + Days + ":" + Hours + ":" + Minutes + ":" + Seconds); 

but my hours and days are coming out to be incorrect

like image 390
Akshat Agarwal Avatar asked Oct 29 '13 19:10

Akshat Agarwal


People also ask

How do you convert milliseconds to days hours minutes seconds in Java?

Show activity on this post. long seconds = timeInMilliSeconds / 1000; long minutes = seconds / 60; long hours = minutes / 60; long days = hours / 24; String time = days + ":" + hours % 24 + ":" + minutes % 60 + ":" + seconds % 60; This will work if you have more than 28 days, but not if you have a negative time.

How do you convert milliseconds into hours minutes and seconds?

To convert milliseconds to hours, minutes, seconds: Divide the milliseconds by 1000 to get the seconds. Divide the seconds by 60 to get the minutes. Divide the minutes by 60 to get the hours. Add a leading zero if the values are less than 10 to format them consistently.


2 Answers

A simple way to calculate the time is to use something like

long seconds = timeInMilliSeconds / 1000; long minutes = seconds / 60; long hours = minutes / 60; long days = hours / 24; String time = days + ":" + hours % 24 + ":" + minutes % 60 + ":" + seconds % 60;  

This will work if you have more than 28 days, but not if you have a negative time.

like image 78
Peter Lawrey Avatar answered Oct 05 '22 18:10

Peter Lawrey


SimpleDateFormat is your friend! (I just discovered it today, it's awesome.)

SimpleDateFormat formatter = new SimpleDateFormat("dd:HH:mm:ss", Locale.UK);  Date date = new Date(timeInMilliSeconds); String result = formatter.format(date); 
like image 40
Aurelien Ribon Avatar answered Oct 05 '22 16:10

Aurelien Ribon