Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

java - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...