Android 倒数计时器:添加小时

在倒数计时器中添加小时数,我只有几分钟然后如何格式化它


 private void updateCountDownText() {

        int hours = (int) (mTimeLeftInMillis / 1000) / 36000;

        int minutes = (int) (mTimeLeftInMillis / 1000) / 60;

        int seconds = (int) (mTimeLeftInMillis / 1000) % 60;

        String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);

        mTextViewCountDown.setText(timeLeftFormatted);

    }


桃花长相依
浏览 177回答 1
1回答

慕斯王

我建议使用TimeUnit而不是手动转换时间。试试这个:int hours = TimeUnit.MILLISECONDS.toHours(mTimeLeftInMillis);int minutes = TimeUnit.MILLISECONDS.toMinutes(mTimeLeftInMillis) % TimeUnit.HOURS.toMinutes(1);int seconds = TimeUnit.MILLISECONDS.toSeconds(mTimeLeftInMillis) % TimeUnit.MINUTES.toSeconds(1);String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);mTextViewCountDown.setText(timeLeftFormatted);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java