0

i am working on a android app just like countdown. at this point i am doing some basic functionalities.

Runnable updater;
    int b = 0;

    void updateTime(final int timeString) // passing milliseconds
      {
        b = timeString;
        timer = (TextView) findViewById(R.id.timerText);
        final Handler timerHandler = new Handler();

        updater = new Runnable() {
            @Override
            public void run() {
                if (b >= 1000) {
                    timer.setText("" + b);
                    //decrease 1 sec
                    b = b - 1000;
                    timerHandler.postDelayed(updater, 1000);
                } else {
                    timerHandler.removeCallbacks(updater);
                }
            }
        };
        timerHandler.post(updater);
    }

in above code i just tried to pass milliseconds as a parameter and then printing the remaining seconds on timer textview. after every seconds.

so its working fine. its updating textview. like suppose i passed 5000 as parameter then output: 5000 4000 3000 and so on..and stops after reaching at 1000

but now the problem is i want to convert those milliseconds to Days:hr:min:sec and then should it print. so, i tried as

Runnable updater;
int b = 0;
void updateTime(final int timeString) {
    b = timeString;
    timer = (TextView) findViewById(R.id.timerText);
    final Handler timerHandler = new Handler();

    updater = new Runnable() {
        @Override
        public void run() {
            if (b >= 1000) {
                int Final=b;
                int day,hr,min,sec;
                day = Final / (60 * 60 * 24);
                Final -= day * (60 * 60 * 24);
                hr = Final / (60 * 60);
                Final -= hr * (60 * 60);
                min = Final / 60;
                sec = Final - min * 60;
                timer.setText(day + " Days " + hr + " Hours " + min + " minutes " + sec + " Sec");

                //decrease 1 sec
                b = b - 1000;
                timerHandler.postDelayed(updater, 1000);
            } else {
                timerHandler.removeCallbacks(updater);
            }
        }
    };
    timerHandler.post(updater);
}

but its not giving correct outpout. i have already used this conversion technique before...but its not working in above case... plz someone tell me, where i am making mistake.please

please note: no i dont want to convert millisec into dd/mm/yyyy format. let say i have 5000 millisec i.s 5 sec. so the handler block should first display 00 days 00 hrs 00 min 5 sec. then after every sec it should decrease 1000 from 5000 i.e 4000 so it will display ........ 04 sec and so on.. until......00 sec ...( just like countdown )

varsha
  • 113
  • 1
  • 1
  • 9
  • Possible duplicate of [How to convert Milliseconds to "X mins, x seconds" in Java?](http://stackoverflow.com/questions/625433/how-to-convert-milliseconds-to-x-mins-x-seconds-in-java) – klauskpm Oct 18 '16 at 17:28
  • ohh may be it will help me. i ll try it – varsha Oct 18 '16 at 17:33
  • define "but its not working" – njzk2 Oct 18 '16 at 18:00
  • plz see my first code,where i just tried to decrease my total seconds by 1..and then displaying... so it was working. but when i tried to convert those seconds to day: hr: min: sec it was giving me wrong output..actually i already used that conversion technique to calculate day:hr:min:sec (without runnable) ,but now in runnable block its calculating wrong. – varsha Oct 19 '16 at 03:54

1 Answers1

0

Take a look at the CountDownTimer class and this answer on StackOverflow.

private static final String FORMAT = "%02d:%02d:%02d";

new CountDownTimer(userSelectedTimeInMillis, 1000) {

    public void onTick(long millisUntilFinished) {

        textView.setText(""+String.format(FORMAT,
                TimeUnit.MILLISECONDS.toHours(millisUntilFinished), TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
                TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
                  TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));              
    }

    public void onFinish() {
        textView.setText("done!");
    }
 }.start();
Community
  • 1
  • 1
Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
  • 1
    Thank you @Srikar Reddy, the above link worked for me. basically my mistake was, the conversion technique i used , was the technique of seconds to days:hr:min:sec... and my total time was in milliseconds... Thank you so much – varsha Oct 19 '16 at 04:15