【问题标题】:Countdown Timer from Date从日期开始的倒数计时器
【发布时间】:2013-12-16 16:15:50
【问题描述】:

我正在尝试使用 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。现在我将日期设置为本月 26 日,即 2013 年。所以目前 26 - 16 = 10 天。这应该大约是显示的天数。那为什么显示40?请帮忙。谢谢大家。

【问题讨论】:

    标签: android date time countdowntimer


    【解决方案1】:

    android.text.format.Time 记录了月份值范围是从 0 到 11,因此 december 在此 api 中应该具有(不直观的)值 11。因此,当您说 12 而不是 11 时,那么您未来的日期是一月,然后您又是 30/31 天后,这可以解释您的结果是 40 而不是 10 天。

    很抱歉你用了这么糟糕的api。

    【讨论】:

    • 谢谢兄弟。我也觉得很抱歉。
    【解决方案2】:

    您必须在

    中进行更正
    FestCountdownTimer timer = new FestCountdownTimer(00, 00, 9, 26, 11,
                2013);
    

    而不是这个:

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

    因为:

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

    设置方法是这样工作的:

    public void set (int second, int minute, int hour, int monthDay, int month, int year) 在 API 级别 3 中添加 设置字段。将 weekDay、yearDay 和 gmtoff 设置为 0,并将 isDst 设置为 -1。如果需要,请调用 normalize(boolean)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 2021-02-03
      相关资源
      最近更新 更多