I want to display count down timer
in HH:MM:SS
format..
I tried many things but none worked
@Override
public void onTick(long millisUntilFinish) {
long millis = millisUntilFinish;
String ms = String.format("%02d:%02d:%02d", millis / 3600,
(millis % 3600) / 60, (millis % 60));
tapHere.setText(ms);
}
In case anybody land here with similar issue, here is the solution :
timer.setText(formatMilliSecondsToTime(passedTime));
private String formatMilliSecondsToTime(long milliseconds) {
int seconds = (int) (milliseconds / 1000) % 60;
int minutes = (int) ((milliseconds / (1000 * 60)) % 60);
int hours = (int) ((milliseconds / (1000 * 60 * 60)) % 24);
return twoDigitString(hours) + " : " + twoDigitString(minutes) + " : "
+ twoDigitString(seconds);
}
private String twoDigitString(long number) {
if (number == 0) {
return "00";
}
if (number / 10 == 0) {
return "0" + number;
}
return String.valueOf(number);
}
there is my solution:
duration=81200000; //6 hours
new CountDownTimer(duration, 1000) {
public void onTick(long millisUntilFinished) {
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long elapsedHours = millisUntilFinished / hoursInMilli;
millisUntilFinished = millisUntilFinished % hoursInMilli;
long elapsedMinutes = millisUntilFinished / minutesInMilli;
millisUntilFinished = millisUntilFinished % minutesInMilli;
long elapsedSeconds = millisUntilFinished / secondsInMilli;
String yy = String.format("%02d:%02d", elapsedHours, elapsedMinutes);
textView.setText(yy);
}
public void onFinish() {
textView.setText("00:00");
}
}.start();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With