How to set Android Chronometer base time from Date object?

半腔热情 提交于 2019-11-30 13:00:44

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

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!