在倒数计时器中增加小时数,我只有几分钟,然后如何格式化
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);
}
答案 0 :(得分:0)
我建议您使用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);