Take the 2-minute tour ×

I am writing a app for which i need to display the Time elapsed when user presses a button in UI screen . So please guide me in writing the code in Android. I need to display the time in seconds, mins and hours.

Thanks in anticipation.

share|improve this question
There's a demo/discussion of this on the android dev blog. –  eldarerathis Oct 15 '10 at 16:56

2 Answers 2

up vote 3 down vote accepted

You can get the current system time in milliseconds by calling System.currentTimeMillis(). You can store this in an variable on Activity start and then calculate the difference between the current time and the stored time in the onClick method of the button.


class ExampleApp exnteds Activity {
    private long startTime;
    public void onCreate(Bundle sis) {
        startTime = System.currentTimeMillis();

    public void onMyButtonClick(View v) {
        long timeGoneMillis = System.currentTimeMillis() - startTime;
        long timeGoneSecs = timeGoneMillis / 1000;
share|improve this answer
Thanks guys for the heads up ! i also found solution for similar problem, so wanted share the URL : stackoverflow.com/questions/2536882?tab=newest#tab-top –  ReachmeDroid Oct 15 '10 at 21:05

Your Answer


