0

What I am looking to do is create a timer that starts counting up from a given timestamp basically telling my how long it has been since this point in time.

an example timestamp would be this 1365679237087

I was looking at the Chronometer but if i set the base to the timestamp it gives me all these ascii characters instead of counting up from the time.

example:

timer = (Chronometer)v.findViewById(R.id.chronometer1);
timer.setBase(1365679237087);
timer.start(); 

is that not how to use the chronometer?

is there another solution to get what I want to do

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • One of mine idea is to read the time when u start timestamp ,then at the time when u stop ,then take the difference and add this to timestamp – Viswanath Lekshmanan Apr 12 '13 at 14:26
  • @Arju doing `long t = incidentStartTime-SystemClock.elapsedRealtime();` will give me the time difference between the two but them doing `timer.setBase(SystemClock.elapsedRealtime() + t);` does not work it still gives me ascii characters – tyczj Apr 12 '13 at 14:34
  • i dont get what u need as your final output in this, can u explain that – Viswanath Lekshmanan Apr 12 '13 at 14:36
  • the answer by Aaron is what i suggested without code – Viswanath Lekshmanan Apr 12 '13 at 14:40
  • @Arju I need to display the time difference and then keep counting up from there to show how long it has been since the event – tyczj Apr 12 '13 at 14:40

2 Answers2

0

I think what Arju suggested would be the simplest.

Just take the time at the start and get the difference at the end. Add this to your timestamp and you should be good.

long start = System.currentTimeMillis();

long difference = System.currentTimeMillis() - start;

long newStamp = 1365679237087 + difference;

I think you might be running into problems because your timestamp uses milliseconds - does Chronometer.setBase() support timestamps with milliseconds?

Edit: Looking through the Chronometer source I see no reference to milliseconds, only seconds.

Aaron
  • 699
  • 4
  • 14
  • it does not say specifically it deals with milliseconds it just takes a long but `SystemClock.elapsedRealtime()` is the time since boot in milliseconds and thats what it seems you need to use – tyczj Apr 12 '13 at 14:42
  • going by this answer however looks to me as it is in milliseconds http://stackoverflow.com/questions/526524/android-get-time-of-chronometer-widget – tyczj Apr 12 '13 at 14:47
  • Try timer.setBase(1365679237087/1000) and see what happens – Aaron Apr 12 '13 at 14:47
  • I get an error `The literal 1365679237087 of type int is out of range ` – tyczj Apr 12 '13 at 14:49
  • problem ended up being i needed to get the difference between the two times, add the difference to the current time `SystemClock.elapsedRealtime()+diff` then add the offset for the timezone which in my hase is 4 hours so its `(SystemClock.elapsedRealtime()+diff)+14400000` – tyczj Apr 12 '13 at 16:33
0

I figured it out, I needed to get the difference between the times, add that to the system time and find the offset of my timezone since the times are in UTC

long system = SystemClock.elapsedRealtime();
long t = incidentStartTime-System.currentTimeMillis();

timer.setBase((system+t)+14400000);
timer.start();
tyczj
  • 71,600
  • 54
  • 194
  • 296