I want to calculate the time in seconds once the activity had started, what kind of library or function should I use? What was the minimal coding I can do that?
Asked
Active
Viewed 29 times
-2
-
Use `System.currentTimeMillis();` at start and end. – CloudPotato Aug 01 '16 at 13:34
-
what if i want seconds only? – Jessica Aug 01 '16 at 13:37
-
this is getting current time in milli second, if i want the time continue counting until i want it to be stop – Jessica Aug 01 '16 at 13:39
-
Do you want a output like `1 2 3 4 5 6 ...`? – CloudPotato Aug 01 '16 at 13:40
-
yes. i want to get exact every second – Jessica Aug 01 '16 at 13:43
1 Answers
0
You can do it like this:
int cnt = 1;
while(condition)
{
//print cnt
Thread.sleep(1000); // wait 1 second
cnt++;
//...
}
But you have to take care, that every command you add in the the do-while loop will take time, too.
Another opportunity is to use a Timer
. I don't have experience with that, but this maybe helps you: Print “hello world” every X seconds

Community
- 1
- 1

CloudPotato
- 1,255
- 1
- 17
- 32
-
Thats just counting iterations. Whats the point of sleeping 1 second? You cant measure elapsed time just extend each iterations duration by 1000ms... – eldo Aug 01 '16 at 13:51
-
Sorry, but you can not edit this anwer in a good way, sleep will just hold it back from doing its job and you still dont know the duration, what if one iteration took 10 seconds for example? You measure delta time like: afterTime - beforeTime. – eldo Aug 01 '16 at 13:59
-
@eldo If you use multi threading for this method it isn't too bad. The OP hasn't added many details so I just made a suggestion – CloudPotato Aug 01 '16 at 14:04
-
I see there arent any details, but op wants to measure time. Your method counts how many times did it sleep for 1 second and this has nothing to do with the actual elapsed time. – eldo Aug 01 '16 at 14:30
-
@eldo For that I suggested that the OP can use `System.currentTimeMillis();` at the start and the end of the method. But for the output every second I suggest the methods from my answer. – CloudPotato Aug 01 '16 at 14:33
-
-
@eldo What do you think is the right way to do this? I know my solution is not exact and changes on runtime, but how can I do it better? – CloudPotato Aug 01 '16 at 14:39
-
WIth System.currentTimeMillis(); as you mentioned to measure time and you can do whatever you want with it. If the difference is 1 sec exaclty than print the current time for example. To do a particular job at a regular time rate also you can use Timer, I was just pointing out that, this sleep stuff is not really doing what you expect. – eldo Aug 01 '16 at 14:47
-
@eldo From the theoretical view you are right, but in praxis you have the problem that the calculation of the current time change in Java takes time itself and you still have the problem that you can not say how long a print-command or a variable declaration takes on runtime. So you don't know if the moment when you print the second on the screen is really a second. All in all this method isn't exact in generell. – CloudPotato Aug 01 '16 at 14:59