gpt4 book ai didi

java - Android 倒计时器 : Add Hour

转载 作者:行者123 更新时间:2023-12-02 10:52:46 26 4
gpt4 key购买 nike

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

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

最佳答案

我建议使用 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);

关于java - Android 倒计时器 : Add Hour,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52028419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com