[英]CountDownTimer not displaying time correctly
我正在為Android開發。 我正在使用倒數計時器來顯示特定日期的倒數。 在18dys 12hrs 30min 55s
本文時,它應顯示接近18dys 12hrs 30min 55s
。 但是我得到49dys 19hrs 28min 31s
分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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.