How to set Android Chronometer base time from Date object?

0

Issue

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?

Solution

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().

Answered By – CommonsWare

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More