gpt4 book ai didi

java - CountDownTimer 无法正确显示时间

转载 作者:行者123 更新时间:2023-12-01 14:16:58 26 4
gpt4 key购买 nike

我正在为 Android 开发。我正在使用倒计时器来显示特定日期的倒计时。在撰写本文时,它应该显示接近 18dys 12hrs 30min 55s 的内容。但我得到了49dys 19hrs 28min 31s。我相信我对可读日期的转换是正确的,但话又说回来,我对这个计时器没有经验。我的研究让我陷入了困境。请查看我的代码并找出我可能存在的任何错误。提前致谢。

private void startCountdown() {
CountDownTimer mCountDownTimer;
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.YEAR, 2013);
cal2.set(Calendar.MONTH, 8);
cal2.set(Calendar.DAY_OF_MONTH, 23);
cal2.set(Calendar.HOUR_OF_DAY, 7);
cal2.set(Calendar.MINUTE, 0);

long diffInMillis = cal2.getTimeInMillis() - cal1.getTimeInMillis();



tvCount = (TextView) findViewById(R.id.tvCountdown);

mCountDownTimer = new CountDownTimer(diffInMillis, 1000) {


@Override
public void onFinish() {
tvCount.setText("VIEW FIGHT CARD");

}

@Override
public void onTick(long millisUntilFinished) {

String days = null, hours = null, minutes = null, seconds = null;
long count = millisUntilFinished / DateUtils.DAY_IN_MILLIS;
if(millisUntilFinished > DateUtils.DAY_IN_MILLIS) {

if(count > 1)
days = (count)+("dys ");
else
days = (count)+("dy ");

millisUntilFinished %= DateUtils.DAY_IN_MILLIS;
}
if(millisUntilFinished > DateUtils.HOUR_IN_MILLIS) {
count = millisUntilFinished / DateUtils.HOUR_IN_MILLIS;
if(count > 1)
hours = (count) + "hrs ";
else
hours = count + "hr ";

millisUntilFinished %= DateUtils.HOUR_IN_MILLIS;
}
if(millisUntilFinished > DateUtils.MINUTE_IN_MILLIS) {
count = millisUntilFinished / DateUtils.MINUTE_IN_MILLIS;

minutes = count + "min";

millisUntilFinished %= DateUtils.MINUTE_IN_MILLIS;
}
if(millisUntilFinished > DateUtils.SECOND_IN_MILLIS) {
count = millisUntilFinished / DateUtils.SECOND_IN_MILLIS;

seconds = count + "s";

millisUntilFinished %= DateUtils.SECOND_IN_MILLIS;
}



tvCount.setText(days + hours + minutes +seconds + "");
}
};
mCountDownTimer.start();
}

最佳答案

不要忘记在 Java 中月份索引为 0

一月 = 0,二月 = 1,...,八月 = 7

使用起来更安全

cal2.set(Calendar.MONTH, Calendar.AUGUST);

关于java - CountDownTimer 无法正确显示时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18045667/

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