11

I've got an issue with starting chronometer from the specific time. There is a Date object I want my chronometer start from:

Date d = new Date(); //now, just for example
chronometer.setBase(d.getTime()); //long value of d
Log.d("Date: " , "d.getTime() time is [" + d.getTime() +"]");
Log.d("Chron: " , "chronometer.getBase() is [" + chronometer.getBase() +"]");
//let's print out elapsedRealtime from official sample
Log.d("Chron: " , "SystemClock.elapsedRealtime() is [" + SystemClock.elapsedRealtime() +"]");

Output:

06-02 13:35:23.025: D/Date:(928): d.getTime() time is [1338644123032]
06-02 13:35:23.037: D/Chron:(928): chronometer.getBase() is [1338644123032]
06-02 13:35:23.037: D/Chron:(928): SystemClock.elapsedRealtime() is [11624388]

Actually, why this long values of time differes (11624388 and 1338644123032)?

When I start my chronometer from base

chronometer.setBase(SystemClock.elapsedRealtime());

- it always works fine ( "00:00" and rising)

But when I try to set the date from a past Date (f.e. yesterday):

chronometer.setBase(yesterday.getTime());

- it shows "00:0(" and changes every second the latest char to ")", "*", "/" and others

Could you please advise how can I set the chronometer base to a Date object?

Vitalliuss
  • 1,614
  • 1
  • 12
  • 10

2 Answers2

35

I actually had a similar problem (the Date was coming from an external service, not the database) and I wanted to show how old the date was.

It proved to be simple:

long lastSuccess = serviceDate.getTime(); //Some Date object
long elapsedRealtimeOffset = System.currentTimeMillis() - SystemClock.elapsedRealtime();
pollAgeView.setBase(lastSuccess - elapsedRealtimeOffset);
pollAgeView.start();
daveespo
  • 589
  • 5
  • 9
  • 3
    This is a much more reasonable approach than rewriting `Chronometer` to accept a `Date` and answers the question better than the current accepted answer. – Hugh Jeffner Mar 25 '16 at 15:39
  • This is not a good ideia, since user could change the date from your device. So how you sincronize a real date time from server without using system base time ? – Juan Munhoes Junior Feb 05 '17 at 21:13
4

Actually, why this long values of time differes (11624388 and 1338644123032)?

SystemClock.elapsedRealtime() is the number of milliseconds since the device was turned on. The other values are based off of System.currentTimeMillis(), the number of milliseconds since the Unix epoch.

Could you please advise how can I set the chronometer base to a Date object?

You don't. That is not what Chronometer is for. Quoting the documentation for Chronometer:

You can give it a start time in the elapsedRealtime() timebase, and it counts up from that, or if you don't give it a base time, it will use the time at which you call start().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks Mark. My goal is to create a digital clock which shows time duration between two activities (stored in database with time record). Could you please advise the best practice for this? – Vitalliuss Jun 02 '12 at 16:08
  • 1
    @Vitalliuss: If both your start and end times are fixed in a database, then just use `TextView`, since your value will not be changing. If your start time is fixed in a database, and you are counting from there, you will need to create your own `Chronometer` equivalent that takes a start time in the `System.currentTimeMillis()` timebase, instead of `elapsedRealtime()`. Since [`Chronometer` is not a very long class](https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/Chronometer.java), cloning it and making your own should be reasonably easy. – CommonsWare Jun 02 '12 at 16:21