Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Countdown timer in HH:MM:SS (Hours : Minutes : Seconds)

Tags:

android

I want to display count down timer in HH:MM:SS format.. I tried many things but none worked

@Override
    public void onTick(long millisUntilFinish) {

        long millis = millisUntilFinish;

        String ms = String.format("%02d:%02d:%02d", millis / 3600,
                (millis % 3600) / 60, (millis % 60));

        tapHere.setText(ms);
    }
like image 857
Hirak Chhatbar Avatar asked Aug 06 '15 06:08

Hirak Chhatbar


2 Answers

In case anybody land here with similar issue, here is the solution :

timer.setText(formatMilliSecondsToTime(passedTime));

private String formatMilliSecondsToTime(long milliseconds) {

    int seconds = (int) (milliseconds / 1000) % 60;
    int minutes = (int) ((milliseconds / (1000 * 60)) % 60);
    int hours = (int) ((milliseconds / (1000 * 60 * 60)) % 24);
    return twoDigitString(hours) + " : " + twoDigitString(minutes) + " : "
            + twoDigitString(seconds);
}

private String twoDigitString(long number) {

    if (number == 0) {
        return "00";
    }

    if (number / 10 == 0) {
        return "0" + number;
    }

    return String.valueOf(number);
}
like image 157
Hirak Chhatbar Avatar answered Nov 05 '22 07:11

Hirak Chhatbar


there is my solution:

   duration=81200000; //6 hours
   new CountDownTimer(duration, 1000) {

        public void onTick(long millisUntilFinished) {
            long secondsInMilli = 1000;
            long minutesInMilli = secondsInMilli * 60;
            long hoursInMilli = minutesInMilli * 60;

            long elapsedHours = millisUntilFinished / hoursInMilli;
            millisUntilFinished = millisUntilFinished % hoursInMilli;

            long elapsedMinutes = millisUntilFinished / minutesInMilli;
            millisUntilFinished = millisUntilFinished % minutesInMilli;

            long elapsedSeconds = millisUntilFinished / secondsInMilli;

            String yy = String.format("%02d:%02d", elapsedHours, elapsedMinutes);
            textView.setText(yy);
        }

        public void onFinish() {

            textView.setText("00:00");
        }
    }.start();
like image 6
Amal Kronz Avatar answered Nov 05 '22 09:11

Amal Kronz