Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Count down timer that shows in HH:MM:SS format

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 2.78k
    Comment on it

    If we need to show countdown till in some point of time in future then CountDown timer is the best solution

    CountDown Timer sets countdown till in some point of time in the future, with regular interval.

    CountDownTimer is the class that handles all this.

    Below is the code that shows countdown on a TextView

     new CountDownTimer(30000, 1000) {
    
         public void onTick(long millisUntilFinished) {
             mCountDownText.setText("Remaining seconds: " + millisUntilFinished / 1000);
         }
    
         public void onFinish() {
             mCountDownText.setText("done!");
         }
      }.start();


    Now we need to show timer in HH:MM:SS format.

    So need to convert the long value to hours, min and second simple.

    I am doing it using TimeUnit that represents time durations at a given unit of granularity and extents Enum<TimeUnit>

    So Below is the modified code.

     new CountDownTimer(100000, 1000) {
                                                            @Override
                                                            public void onTick(long millisUntilFinished) {
    
    
                                                              String timerValue =   String.format("%02d:%02d:%02d",
                                                                        TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
                                                                        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) -
                                                                                TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), // The change is in this line
                                                                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                                                                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
    
                                                                String timerValue1 = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
                                                                        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
                                                                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
                                                                System.out.println(hms);
                                                                mTimerButton.setText(timerValue1);
                                                            }
    
                                                            @Override
                                                            public void onFinish() {
                                                             mTimerButton.setText("done!");
                                                            }
                                                        }.start();

     

 1 Comment(s)

  • Hello ,

    You can use this method to show the time in HH:MM:SS format. You can also download the source code of Countdown timer in android from here

    private void fn&#95;countdown(long time){
        countDownTimer = new CountDownTimer(time, sec) {
            @Override
            public void onTick(long millisUntilFinished) {
    
                onpause = millisUntilFinished;
                int seconds = (int) (millisUntilFinished / 1000) % 60;
                int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
                int hours = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
                String newtime = hours + ":" + minutes + ":" + seconds;
    
                if (newtime.equals("0:0:0")) {
                    tv&#95;time.setText("00:00:00");
                } else if ((String.valueOf(hours).length() == 1) && (String.valueOf(minutes).length() == 1) && (String.valueOf(seconds).length() == 1)) {
                    tv&#95;time.setText("0" + hours + ":0" + minutes + ":0" + seconds);
                } else if ((String.valueOf(hours).length() == 1) && (String.valueOf(minutes).length() == 1)) {
                    tv&#95;time.setText("0" + hours + ":0" + minutes + ":" + seconds);
                } else if ((String.valueOf(hours).length() == 1) && (String.valueOf(seconds).length() == 1)) {
                    tv&#95;time.setText("0" + hours + ":" + minutes + ":0" + seconds);
                } else if ((String.valueOf(minutes).length() == 1) && (String.valueOf(seconds).length() == 1)) {
                    tv&#95;time.setText(hours + ":0" + minutes + ":0" + seconds);
                } else if (String.valueOf(hours).length() == 1) {
                    tv&#95;time.setText("0" + hours + ":" + minutes + ":" + seconds);
                } else if (String.valueOf(minutes).length() == 1) {
                    tv&#95;time.setText(hours + ":0" + minutes + ":" + seconds);
                } else if (String.valueOf(seconds).length() == 1) {
                    tv&#95;time.setText(hours + ":" + minutes + ":0" + seconds);
                } else {
                    tv&#95;time.setText(hours + ":" + minutes + ":" + seconds);
                }
    
    
            }
    
            @Override
            public void onFinish() {
                tv&#95;time.setText(00 + ":" + 00 + ":" + 00);
            }
        }.start();
    }
    
    
    }
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: