Domanda

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.

È stato utile?

Soluzione

On android.text.format.Time it is documented that the month value range is from 0 to 11, so december should have the (unintuitive) value 11 in this api. So when you say 12 instead of 11, so your future date is in january, and then you are additional 30/31 days later which could explain your result of 40 instead of 10 days.

I feel sorry for you to use such a bad api.

Altri suggerimenti

You have to made correction in

FestCountdownTimer timer = new FestCountdownTimer(00, 00, 9, 26, 11,
            2013);

instead of this :

FestCountdownTimer timer = new FestCountdownTimer(00, 00, 9, 26, 12,
            2013);

because :

void android.text.format.Time.set(int second, int minute, int hour, int monthDay, int month, int year)

Set Method work like this :

public void set (int second, int minute, int hour, int monthDay, int month, int year) Added in API level 3 Sets the fields. Sets weekDay, yearDay and gmtoff to 0, and isDst to -1. Call normalize(boolean) if you need those.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top