0

I'm trying to do a simple Timer using Android Chronometer. However the timer is starting 18:00:00, not 00:00:00. Can anyone explain why is this occurring please?

Here's the code:

final Chronometer mChronometer = (Chronometer) retView.findViewById(R.id.chronometer);
mChronometer.start();
mChronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
                @Override
                public void onChronometerTick(Chronometer chrono) {
                    long t = SystemClock.elapsedRealtime() - chrono.getBase();
                    chrono.setText(DateFormat.format("kk:mm:ss", t));
                }
            });

Here's the xml of it:

        <Chronometer
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/chronometer"
        android:textSize="28sp"/>

EDIT:

I added 21600000 (6 hours in milliseconds) to my t, it evens out the time. But this feels hacky and potentially result in error down the line. If someone has better way to deal with please help.
long t = SystemClock.elapsedRealtime() - chrono.getBase() + 21600000;

Unu
  • 671
  • 8
  • 17
  • 2
    How far is your timezone from GMT? I mean how many hours on the device you are testing on? – Squonk Mar 10 '15 at 17:00
  • I left it for an hour it goes to 07:00:00, I'm in GMT -6 time. I see what you mean, any clue on how to resolve for timezone if that's the issue? – Unu Mar 10 '15 at 17:21
  • I added 21600000 to my t, it evens out the time. But this feels hacky and potentially result in error down the line. If someone has better way to deal with please help. `long t = SystemClock.elapsedRealtime() - chrono.getBase() + 21600000;` – Unu Mar 10 '15 at 17:38
  • What DateFormat class are you using? Neither the Java nor the Android DateFormat class contains a static format() method. – Kevin Workman Mar 10 '15 at 17:43
  • @Tyler : The problem is you are using DateFormat which assumes you are using an absolute time hence it is adjusting for timezone / DST which is obviously not what you want for a Chronometer which only ever needs to show relative time of 00:00:00 (0hrs, 0mins, 0secs) to 99:59:59 (99hrs, 59mins,59secs). You need to rethink how you do this perhaps with a Sting format of some sort rather than DateFormat. By the way, the Chronometerr class should be capable of displaying it's own time - why are you formatting / displaying it yourself? – Squonk Mar 11 '15 at 03:32
  • @Tyler : One other good reason for *NOT* using a DateFormat...`kk` displays hours in values of 01-24 format with `HH` as 00-23. Either way if you run the Chronometer for more than a day it'll either rollover the hour field or cause a Parse exception as DateFormat has no concept of displaying 25, 26, 27 etc hours in an hour field of a time string. – Squonk Mar 11 '15 at 03:41
  • @Squonk Thanks! I was using DateFormat to get full hours of the TextView(Chronometer) visible. I didn't think it was possible to just use String. And HH didn't work, it resulted in HH:00:00 for some reason. – Unu Mar 11 '15 at 18:38

1 Answers1

1

I think you can use SimpleTimeZone.getDefault().getRawOffset() to get the offset in milliseconds from UTC of this time zone's standard time.

for more information please look at: http://developer.android.com/reference/java/util/TimeZone.html#getRawOffset()

kaho
  • 4,746
  • 1
  • 16
  • 24
  • Thanks. That works pretty good. RawOffSet returns "-21600000" so be careful with subtraction and addition. – Unu Mar 10 '15 at 18:29
  • I think you can simply add the value since it is "Offset" :) – kaho Mar 12 '15 at 22:05