0

I have a countdown timer and i want to print in hours, minutes and seconds. I have the following code but the output I'm getting is not properly converted in minutes I don't understand where I am going wrong.

enter code here 
CountDownTimer countDownTimer = new CountDownTimer(millisset3, 1000) 

          {
               TextView  tv = (TextView) context.findViewById(R.id.textView2); 
               public void onFinish() {

                     tv.setText("Time finished!");   }

                     public void onTick(long millisUntilFinished) {

                         tv.setText("Time left for Isha:   " +String.format(" %d hours, %d min, %d sec", 
                                    TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
                                    TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished) - 
                                    TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours( millisUntilFinished)),
                                    TimeUnit.MILLISECONDS.toSeconds( millisUntilFinished) - 
                                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes( millisUntilFinished)))); 
                                 }

       }.start(); 

        }
Anita
  • 49
  • 2
  • 11
  • This question already posted in this link plz check answer:http://stackoverflow.com/questions/17620641/countdowntimer-in-minutes-and-seconds – Sanket990 May 06 '14 at 12:06

2 Answers2

0

try this

    private long startTime = 0L;
         private Handler myHandler = new Handler();
         long timeInMillies = 0L;
         long timeSwap = 0L;
         long finalTime = 0L;


        //on button click(timer start)

         startTime = SystemClock.uptimeMillis();
            myHandler.postDelayed(updateTimerMethod, 0);

        //=====

//on button click (timer stop)

 timeSwap += timeInMillies;
    myHandler.removeCallbacks(updateTimerMethod);

//==================


        private Runnable updateTimerMethod = new Runnable() {

          public void run() {
           timeInMillies = SystemClock.uptimeMillis() - startTime;
           finalTime = timeSwap + timeInMillies;

           int seconds = (int) (finalTime / 1000);
           int minutes = seconds / 60;
           seconds = seconds % 60;
           int milliseconds = (int) (finalTime % 1000);
           Log.e("" + minutes + ":"
             + String.format("%02d", seconds) + ":"
             + String.format("%03d", milliseconds));
           myHandler.postDelayed(this, 0);
          }

         };
Santosh Kathait
  • 1,444
  • 1
  • 11
  • 22
  • I have to do this using the countdown timer. I am doing a wrong conversion can you show me that how I can convert. – Anita May 06 '14 at 12:09
0

You can pass the milliseconds to this function and you will get a string in the format.

public static String getFormat(long millis)
    {

        DateFormat outFormat = new SimpleDateFormat("hh:mm:ss");
        outFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        Date d = new Date(millis);
        String result = outFormat.format(d);

        return result;
    }
Sreedev
  • 6,563
  • 5
  • 43
  • 66