我試圖使用Android CountdownTimer
類和Java Time
從當天,小時,分鐘,秒到2013年12月26日上午9:00倒數實現'日期'倒計時器。下面是我的代碼:從日期倒數計時器
FestCountdownTimer timer = new FestCountdownTimer(00, 00, 9, 26, 12, 2013);
new CountDownTimer(timer.getIntervalMillis(), 1000) {
@Override
public void onTick(long millisUntilFinished) {
int days = (int) ((millisUntilFinished/1000)/86400);
int hours = (int) (((millisUntilFinished/1000)
- (days * 86400))/3600);
int minutes = (int) (((millisUntilFinished/1000)
- (days * 86400) - (hours * 3600))/60);
int seconds = (int) ((millisUntilFinished/1000) % 60);
String countdown = String.format("%02dd %02dh %02dm %02ds", days,
hours, minutes, seconds);
countdownTimer.setText(countdown);
}
@Override
public void onFinish() {
countdownBegins.setVisibility(View.GONE);
countdownTimer.setText("IT'S HERE!");
}
}.start();
這裏是我的FestCountdownTimer
類:
public class FestCountdownTimer {
private long intervalMillis;
public FestCountdownTimer(int second, int minute, int hour, int monthDay, int month, int year) {
Time futureTime = new Time();
// Set date to future time
futureTime.set(second, minute, hour, monthDay, month, year);
futureTime.normalize(true);
long futureMillis = futureTime.toMillis(true);
Time timeNow = new Time();
// Set date to current time
timeNow.setToNow();
timeNow.normalize(true);
long nowMillis = timeNow.toMillis(true);
// Subtract current milliseconds time from future milliseconds time to retrieve interval
intervalMillis = futureMillis - nowMillis;
}
public long getIntervalMillis() {
return intervalMillis;
}
}
現在的小時,分鍾和秒的罰款。這只是天數到了40天。現在我將日期設置爲2013年的本月26日,因此目前26 - 16 = 10天。這大概應該是顯示的天數。那爲什麼會顯示40?請幫忙。感謝大家。
感謝兄弟。我也很抱歉。 – SynerFlow