Reputation: 2726
I'm making somekind of stopwatch and with this code i'm calculating elapsed time:
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (millis%1000);
int hours = minutes / 60;
labelTime.setText(String.format("%02d", hours) + ":" + String.format("%02d", minutes) + ":" + String.format("%02d",seconds)+ ":" + String.format("%03d",milliseconds));
This works, but after 1 hour my stopwatch shows for example 01:61:22 instead of 01:01:22. How to start minutes from 0?
Upvotes: 1
Views: 402
Reputation: 2143
labelTime.setText(String.format("%02d", hours) + ":" + String.format("%02d", minutes-(hours*60)) + ":" + String.format("%02d",seconds)+ ":" + String.format("%03d",milliseconds));
Upvotes: 2
Reputation: 1017
why don't you just use something like:
if(minutes>59){
minutes = 0;
}
Upvotes: 2
Reputation: 6715
This will give you what you are expecting
if(minutes > 59){
minutes = 60 - minutes;
hours = hours + 1;
}
Upvotes: 1