I'm trying to implement a 'date' countdown timer using the Android CountdownTimer class and Java Time which counts down from the current day, hour, minute, second to 26th December, 2013 9:00 AM. Below is my code:
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();
And here is my FestCountdownTimer class:
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;
}
}
Now the hours, minutes, and seconds are fine. It's just that the number of days is coming 40. Now I set the date to 26th of this month, 2013. So currently 26 - 16 = 10 days. That should approximately be the number of days displayed. Then why is it showing 40? Please help. Thanks everyone.