2

I have a problem with the chronometer. When I start it, it begins from 01:00:00. I don't know why. I think that my code is correct.

Can you understand what the problem is?

This is my code:

Chronometer crono = new Chronometer(this);
crono.setBase(SystemClock.elapsedRealtime());
crono.start();

When I print the time I call this method:

long time = SystemClock.elapsedRealtime() - totalTime.getBase();
Date date = new Date(time);
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
formatter.format(date);

Thanks very much!

Deca
  • 153
  • 1
  • 11

1 Answers1

0

You're trying to format a date difference as a date. I'd say maybe timezones come into play?

scala> new java.util.Date(0)
res2: java.util.Date = Thu Jan 01 01:00:00 GMT 1970

scala> new SimpleDateFormat("HH:mm:ss").format(new Date(0))
res5: String = 01:00:00

You might want something like [DurationFormatUtils.formatDuration()](http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DurationFormatUtils.html#formatDuration(long, java.lang.String)).

Vlad
  • 18,195
  • 4
  • 41
  • 71