0

I am developing for Android. I am using a countdown timer to display a countdown to a certain date. at the time of this post, it should be displaying something close to 18dys 12hrs 30min 55s. But I am getting 49dys 19hrs 28min 31s. I believe my conversion to a readable date is correct, but then again, I have no experience with this timer. My research is running me in circles. Please take a look at my code and find any errors I may have. Thanks in advance.

private void startCountdown() {
    CountDownTimer mCountDownTimer;
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal2.set(Calendar.YEAR, 2013);
        cal2.set(Calendar.MONTH, 8);
        cal2.set(Calendar.DAY_OF_MONTH, 23);
        cal2.set(Calendar.HOUR_OF_DAY, 7);
        cal2.set(Calendar.MINUTE, 0);

    long diffInMillis = cal2.getTimeInMillis() - cal1.getTimeInMillis();



        tvCount = (TextView) findViewById(R.id.tvCountdown);

        mCountDownTimer = new CountDownTimer(diffInMillis, 1000) {


            @Override
            public void onFinish() {
                tvCount.setText("VIEW FIGHT CARD");

            }

            @Override
            public void onTick(long millisUntilFinished) {

                String  days = null, hours = null, minutes = null, seconds = null;
                long count = millisUntilFinished / DateUtils.DAY_IN_MILLIS;
                if(millisUntilFinished > DateUtils.DAY_IN_MILLIS) {

                    if(count > 1)
                        days = (count)+("dys ");
                    else
                        days = (count)+("dy ");

                    millisUntilFinished %= DateUtils.DAY_IN_MILLIS;
                }
                if(millisUntilFinished > DateUtils.HOUR_IN_MILLIS) {
                    count = millisUntilFinished / DateUtils.HOUR_IN_MILLIS;
                    if(count > 1)
                       hours = (count) + "hrs ";
                    else
                        hours = count + "hr ";

                    millisUntilFinished %= DateUtils.HOUR_IN_MILLIS;
                }
                if(millisUntilFinished > DateUtils.MINUTE_IN_MILLIS) {
                    count = millisUntilFinished / DateUtils.MINUTE_IN_MILLIS;

                    minutes = count + "min";

                    millisUntilFinished %= DateUtils.MINUTE_IN_MILLIS;
                }
                if(millisUntilFinished > DateUtils.SECOND_IN_MILLIS) {
                    count = millisUntilFinished / DateUtils.SECOND_IN_MILLIS;

                    seconds = count + "s";

                    millisUntilFinished %= DateUtils.SECOND_IN_MILLIS;
                }



                tvCount.setText(days + hours + minutes +seconds + "");
            }
        };
    mCountDownTimer.start();
    }
4

1 回答 1

1

Don't forget in Java the month is 0 indexed so

January = 0, February = 1, ..., August = 7

Much safer to use

cal2.set(Calendar.MONTH, Calendar.AUGUST);
于 2013-08-04T17:40:04.750 回答