0

I have Calendar Object

Calendar cal = Calendar.getInstance();
int hh = cal.get(Calendar.HOUR);
int mm = cal.get(Calendar.MINUTE);
int ss = cal.get(Calendar.SECOND);

And to format time

SimpleDateFormat sdf = new SimpleDateFormat("h:mm:s a", Locale.US);

I want my TextView to display count down time in hh:mm:ss format

Time left: 03:25:12
Time left: 03:25:11
Time left: 03:25:10 
Time left: 03:25:09
.
.
. so on and stop the countdown after 3 hours, 25 minutes and 8 seconds.

After times is lapsed the TextView should display Time left: 00:00:00.

Shadab Mehdi
  • 604
  • 10
  • 23

1 Answers1

2

use the Timer class as:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

long maxTimeInMilliseconds = 3*25*8*1000;// in your case

startTimer(maxTimeInMilliseconds, 1000);

}

public void startTimer(final long finish, long tick)
    {
        t = new CountDownTimer(finish, tick) {

        public void onTick(long millisUntilFinished) 
        {           
            long remainedSecs = millisUntilFinished/1000;
            timer.setText(""+(remainedSecs/60)+":"+(remainedSecs%60));// manage it accordign to you
        }

        public void onFinish() 
        {
             timer.setText("00:00:00");
             cancel();
        }
        }.start();
    }
Akshay Paliwal
  • 3,718
  • 2
  • 39
  • 43