Reputation: 16609
I have timer for a task. And all the sessions will be added up to each other.
Let's say today user spent 5 minutes
Other day he spent 1 hour, here this one hour will be added to the 5 minutes
and so on..
So it will be the total time in one value..
How can I do this ? Is it by Milliseconds or Date object ?
Upvotes: 0
Views: 64
Reputation: 2291
You can keep track of all the milliseconds using Date().getTime(), which returns the time since Epoch. So whenever you need to add/remove, just take your Date object, invoke .getTime(), and add/remove from the total. Then when you're done, you can convert the milliseconds to whatever format you need.
Upvotes: 0
Reputation: 16354
Date
is more useful when you are dealing with actual calendar dates.
If you just want to keep track of time intervals/durations, just have a long
variable and keep on adding the durations to this.
EDIT : Long.MAX_VALUE
is 9,223,372,036,854,775,807. So, you don't really need to worry about the overflow either.
Upvotes: 1