android倒计时器时间

时间:2012-09-18 11:12:47

标签: android timer countdowntimer

我一直在使用Android倒数计时器示例来创建某个日期的倒计时。

Time TimerSet = new Time();
        TimerSet.set(20, 8, 2012); //day month year
        TimerSet.normalize(true);
        long millis = TimerSet.toMillis(true);

        Time TimeNow = new Time();
        TimeNow.setToNow(); // set the date to Current Time
        TimeNow.normalize(true);
        long millis2 = TimeNow.toMillis(true);

        long millisset = millis - millis2; //subtract current from future to set the time remaining

        final int smillis = (int) (millis); //convert long to integer to display conversion results
        final int smillis2 = (int) (millis2);

        new CountDownTimer(millisset, 1000) {
            public void onTick(long millisUntilFinished) {

                // decompose difference into days, hours, minutes and seconds 
                int weeks = (int) ((millisUntilFinished / 1000) /
                        604800);
                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);
                int millicn = (int) (millisUntilFinished / 1000);



                w.setText(" " +weeks);
                d.setText(" " +days);
                h.setText(" " +hours);
                m.setText(" " +minutes);
                s.setText(" " +seconds);
                mTextField.setText(smillis + " " + smillis2 + " " +
                        millicn + "Time remaining: " +weeks +"weeks " +days + " days " + hours
                        + " hours: " + minutes+ " minutes: "
                        + seconds + " seconds: " );

            }

            public void onFinish() {
                mTextField.setText("done!");
            }
        }.start();

我想知道如何设定时间以及日期?目前正在午夜时分。我喜欢它是15:00

提前致谢

1 个答案:

答案 0 :(得分:6)

这是你的问题:

timerSet.set(20, 8, 2012); //day month year

这设置了未来的日期,但不是未来的时间(相反,它将布尔值allDay设置为true),因此它默认为00:00。

来自android API

set(int second, int minute, int hour, int monthDay, int month, int year)

所以试试

timerSet.set(0,0,15,20,8,2012)

2012年8月20日15:00。