android countdown timer time
Asked Answered
I

1

7

I've been using an android countdown timer sample to create a countdown to a certain date.

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();

i was wondering how can i set the time aswell as the date? currently it is ocunting down to midnight. i'd like it to be 15:00

thanks in advance

Ikon answered 18/9, 2012 at 11:12 Comment(5)
hi. sorry for confusion. the app countsdown to 20 september 2012 00:00 (so midnight(00:00) on 20 september 2012 mTextField will say doneIkon
i have tried to change them but the countdown stopsIkon
millis 2 is the current date and time, millis is the current date. if i change millis, it stops the countdownIkon
if millisis the future date, then you have to make sure that it's not set to 00:00 as well.Uel
"TimerSet.toMillis" and timerSet is "TimerSet.set(20, 8, 2012);"Ikon
U
7

Here's your problem:

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

This sets the future date, but not the future time of day (instead, it sets the boolean allDay to true), so it defaults to 00:00.

From the android API:

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

So try

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

for 15:00, August 20th, 2012.

Uel answered 18/9, 2012 at 11:52 Comment(1)
aha. i was doing it wrong way round. i had timerSet.set(15,00,00,20,8,2012)Ikon

© 2022 - 2024 — McMap. All rights reserved.