Search code examples
androidtimeroutputcountdown

how do you properly setup android countdown such that it will always have 2 digits places


how do you properly setup android countdown output such that it will always have 2 digits places?

For example: 02:03 or 02:05 would be the correct output

Current output: 2:3 or 2:5

I tried two implementations that output the same thing:

g.drawString("TIME: " +  ((milliSec / (1000*60)) % 60) + " : " + ((milliSec / 1000) % 60 ), 150, 110);


g.drawString("TIME: " + String.format("%d : %d ",
        TimeUnit.MILLISECONDS.toMinutes( milliSec),
        TimeUnit.MILLISECONDS.toSeconds(milliSec) -
                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliSec))),150,110);
g.drawString("SCORE: " + board.getScore(), 150,50);

Solution

  • In String.format, use String.format("%02d:%02d", ... instead.