0

I've been attempting to format a countdown timer and haven't had much success. The timer counts down the correct amount of minutes/hours/seconds as input, however, upon output back to the user instead of a beautiful normal looking countdown timer look I get the following different outputs: When attempting to countdown from 25 minutes

When attempting to countdown from 25 minutes, then below I have when I attempt to countdown from 10 minutes: 10 minutes It appears to countdown properly with anything 1 minute or less.

This is the code I have to format the output, I have also included the code for the CounterClass which it is in.

private class CounterClass extends CountDownTimer {
    public CounterClass(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        String hms = String.format("%02d:%02d:%02d",
                TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
                TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished));

        timeDisp.setText(hms);
    }
}

Thanks in advance!

Caoimhe
  • 9
  • 4

1 Answers1

0

As stated in this answer

You are able to obtain the hours by doing:

int hours   = (int) ((millisUntilFinished / (1000*60*60)) % 24);

And notice that you can calculate any given amount of minutes by doing this:

_tv.setText(""+String.format("%d min, %d sec", 
                TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished),
                TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
Community
  • 1
  • 1