如何使用android应用中的CountDownTimer修复错误

时间:2019-05-03 18:26:17

标签: java android countdowntimer

我正在尝试从开始日期起以天,小时,分钟,秒为单位进行倒计时,但由于某种原因我找不到正确的日子。

    String givenDateString = "2019-05-15T09:00:00";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    try {
        Date mDate = sdf.parse(givenDateString);
         timeInMilliseconds = mDate.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    new CountDownTimer(timeInMilliseconds, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            long day = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
            millisUntilFinished -= TimeUnit.DAYS.toMillis(day);

            long hour = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
            millisUntilFinished -= TimeUnit.HOURS.toMillis(hour);

            long minute = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
            millisUntilFinished -= TimeUnit.MINUTES.toMillis(minute);

            long second = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);

            prueba.setText("Days: "+day+" Hours: "+hour+" Minutes: "+minute+" Seconds: "+second);
        }

        @Override
        public void onFinish() {
            // What ever you want !
        }

    }.start();

我得到这个结果:

enter image description here

新错误android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。

TextView prueba;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    prueba = findViewById(R.id.prueba);

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date currentDate = null;
            Date destinationDate = null;
            try {
                currentDate = Calendar.getInstance().getTime();
                destinationDate = sdf.parse("2019-05-15T09:00:00");

            } catch (ParseException e) {
                e.printStackTrace();
            }
            calculateDifference(currentDate, destinationDate);
        }
    }, 0, 1000);//Update text every second
}

public void calculateDifference(Date startDate, Date endDate) {
    long different = endDate.getTime() - startDate.getTime();

    long secondsInMilli = 1000;
    long minutesInMilli = secondsInMilli * 60;
    long hoursInMilli = minutesInMilli * 60;
    long daysInMilli = hoursInMilli * 24;

    long days = different / daysInMilli;
    different = different % daysInMilli;

    long hours = different / hoursInMilli;
    different = different % hoursInMilli;

    long minutes = different / minutesInMilli;
    different = different % minutesInMilli;

    long seconds = different / secondsInMilli;

    Log.e("calculation", "Days: " + days + " Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds);
    prueba.setText("Days: " + days + " Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds);
}

1 个答案:

答案 0 :(得分:0)

您好,这是实现此目标的方法

这里是计算两个日期之间的差异的方法。

public void calculateDifference(Date startDate, Date endDate) {
    long different = endDate.getTime() - startDate.getTime();

    long secondsInMilli = 1000;
    long minutesInMilli = secondsInMilli * 60;
    long hoursInMilli = minutesInMilli * 60;
    long daysInMilli = hoursInMilli * 24;

    long days = different / daysInMilli;
    different = different % daysInMilli;

    long hours = different / hoursInMilli;
    different = different % hoursInMilli;

    long minutes = different / minutesInMilli;
    different = different % minutesInMilli;

    long seconds = different / secondsInMilli;

    Log.e("calculation", "Days: " + days + " Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds);

}

您可以设置文本视图的文本而不是日志

这就是您可以通过日期调用计时器

Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            Date currentDate = null;
            Date destinationDate = null;
            try {
                currentDate = Calendar.getInstance().getTime();
                destinationDate = sdf.parse("2019-05-15T09:00:00");

            } catch (ParseException e) {
                e.printStackTrace();
            }

            calculateDifference(currentDate, destinationDate);

        }

    }, 0, 1000);//Update text every second
}