1

I have a Chronometer, which should have the format "hh:mm:ss". The current code give me a time like "0:12052:0-723107" for elapsed 12 seconds. Which is wrong in several ways. The division by 1000 seems not working and the seconds are displayed in the minutes segment.

I already tried String.valueOf but it also didn't work. Here's the current code:

    class updateStoptime implements Runnable {

        @Override
        public void run() {
            chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
                @Override
                public void onChronometerTick(Chronometer chronometer) {

                    timeElapsed = SystemClock.elapsedRealtime() - chronometer.getBase();

                    h = (int) (timeElapsed / 3600000);
                    m = (int) (timeElapsed - h * 3600000 / 60000);
                    s = (int) (timeElapsed - h * 3600000 - m * 60000) / 1000;

                    stopTime = (h < 10 ? "0"+h: h)+":"+(m < 10 ? "0"+m: m)+":"+ (s < 10 ? "0"+s: s);
                    chronometer.setText(stopTime);

                    Log.i("Zeit:", String.valueOf(stopTime));

                }
            });

What do I have to change to get the desired format "hh:mm:ss" ?

Spinshot
  • 275
  • 2
  • 12
  • 1
    Why do you set the chronometer's value during every tick? You use a `Chronometer` by setting the base time once only, then calling `start()` – PPartisan Nov 24 '19 at 12:53
  • Because otherwise its sets back to the original Format on every new tick – Spinshot Nov 24 '19 at 13:02
  • I see. The `Chronometer` class is quite inflexible when it comes to formatting. I think you should use one of the existing Java SDK methods for formatting time however - is [this answer of any use to you?](https://stackoverflow.com/a/4897686/1219389) – PPartisan Nov 24 '19 at 13:06

0 Answers0