I have a small android problem, I have a requirement to have a timer to calculate the duration from the time a specific activity was open till a certain button in that activity is clicked, simply how long the activity was open. While googling around I found TimerTask but this seems to run a thread for a certain interval only though and doesent seem ideal for the job from my little Android experience
Any idea on how to calculate the duration? Preferably in a very simple manner
Any help is very welcome
Regards, MilindaD
What is the Duration Formula? The formula for the duration is a measure of a bond's sensitivity to changes in the interest rate, and it is calculated by dividing the sum product of discounted future cash inflow of the bond and a corresponding number of years by a sum of the discounted future cash inflow.
Duration is how long something lasts, from beginning to end. A duration might be long, such as the duration of a lecture series, or short, as the duration of a party.
A Duration measures an amount of time using time-based values (seconds, nanoseconds). A Period uses date-based values (years, months, days). Note: A Duration of one day is exactly 24 hours long.
Just use System.currentTimeMillis()
to capture the time when the activity starts and stops. E.g.:
long startTime = System.currentTimeMillis();
// wait for activity here
long endTime = System.currentTimeMillis();
long seconds = (endTime - startTime) / 1000;
As of Java 8 there is more convenient way of doing this.
Instant start = Instant.now();
...
Duration.between(start, Instant.now())
Benefit of this approach is more flexible API provided by the Duration class.
https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With