以HH显示倒数计时器:MM:SS(小时:分钟:秒)

时间:2015-08-06 06:28:55

标签: android

我希望以count down timer格式显示HH:MM:SS .. 我尝试过很多东西但没有工作

@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);
    }

3 个答案:

答案 0 :(得分:7)

如果有人在这里遇到类似的问题,这里有解决方案:

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);
}

答案 1 :(得分:3)

有我的解决方案:

   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();

答案 2 :(得分:-2)

您可以通过此current date and time

获取line of code
//Get Current Date & Time
    SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy HH:MM:SS");
    public String currentDateandTime = sdf.format(new Date());