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

android

Solution

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.

Problem

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 Why is the countdown not started? Any Suggestions are appreciated.

Original source

Related problems