Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Countdown issue while displaying days hours minutes seconds using count down timer in android

Tags:

android

I am using countdown timer in my application where i need to display upcoming date in days hours minutes and seconds left.I got the days,hours,minutes and seconds but when i set it to text view the countdown doesn't start.The below is my code.

Date date = new Date(2013,Integer.parseInt(datess.get(k).split("-")[1])-1,Integer.parseInt(datess.get(k).split("-")[0]),hours,mins,secs);  
     long dtMili = System.currentTimeMillis();  
     Date dateNow = new Date(dtMili);  
      remain = date.getTime() - dateNow.getTime();


MyCount counter = new MyCount(remain,1000);
            counter.start();

public class MyCount extends CountDownTimer{
    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        }



    @Override
    public void onFinish() {
        // TODO Auto-generated method stub

        tv3.setText("done");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub


        tv3.setText(timeCalculate(millisUntilFinished/1000) + " Countdown");

    }
}

 public String timeCalculate(long ttime)   
   {  
     long  daysuuu,hoursuuu, minutesuuu, secondsuuu;  
     String daysT = "", restT = "";  



     daysuuu = (Math.round(ttime) / 86400);  
     hoursuuu = (Math.round(ttime) / 3600) - (daysuuu * 24);  
     minutesuuu = (Math.round(ttime) / 60) - (daysuuu * 1440) - (hoursuuu * 60);  
     secondsuuu = Math.round(ttime) % 60;  


     if(daysuuu==1) daysT = String.format("%d day ", daysuuu);  
     if(daysuuu>1) daysT = String.format("%d days ", daysuuu);  

     restT = String.format("%02d:%02d:%02d", hoursuuu, minutesuuu, secondsuuu);  

     return daysT + restT;  
   }  

This is the output

enter image description here

Why is the countdown not started? Any Suggestions are appreciated.

like image 651
hemanth kumar Avatar asked Jan 21 '26 11:01

hemanth kumar


2 Answers

You are not using the millisUntilFinished parameter to update your time in timeCalculate(). Start with:

@Override
public void onTick(long millisUntilFinished) {
    tv3.setText(millisUntilFinished + " Countdown");
}

Once you've confirmed that the timer is working, you'll need a method to convert millisUntilFinished into a human readable String.

The classes related to dates and times in Java are unnecessarily difficult to work with and have some obscure errors. (For instance much of the Date class is deprecated, but the recommended Calendar class still relies on Date heavily...) The 3rd party library Joda Time is a popular replacement.


Addition

the output is not what i expected

25,000 to 700 days in the future is not what I would expect either, date appears to be wrong. As gabriel points out in another answer to this question the year value is calculated from 1900. Though something else is still wrong since 25,000 days is less than 70 years in the future...
However this constructor is deprecated and shouldn't be used, Calendar is the recommended class. I wrote a quick demo using Calendar for you.

But understand that CountDownTimer itself has a couple fundamental flaws:

  • Every time onTick() is called CDT adds a few milliseconds to the overall time, I noticed that it can easily add 7 and half minutes every day.
  • Because of the first error the last onTick() might no be called. When counting down from 5, I typically see "5, 4, 3, 2, <long pause>" then onFinished() displays "0"...

I rewrote CDT in a previous question: android CountDownTimer - additional milliseconds delay between ticks.

like image 153
Sam Avatar answered Jan 22 '26 23:01

Sam


Your problem is that you are not using Date() properly.

The parameters for java.util.Date.Date(int year, int month, int day, int hour, int minute, int second), uses 0 in the first parameter to indicate the year 1900.

The code you wrote calculates a date almost 2000 years into the future, then subtracts current time, which makes for a very long countdown ;-).

The app should work if you change the one line to

Date date = new Date(2013-1900,Integer.parseInt(datess.get(k).split("-")[1])-1,Integer.parseInt(datess.get(k).split("-")[0]),hours,mins,secs);  
like image 42
gabriel Avatar answered Jan 23 '26 00:01

gabriel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!