Android Countdown Timer to Date

Viewed 28540

I am trying to make a countdown timer for a game/date in android. I want to create a timer that displays the days, hours, minutes, and seconds to a date I specify with a final variable. The timer then sets text views to show the days, hours, minutes, and seconds to the user.

Any suggestions about how I could code this?

7 Answers

This is an example in kotlin also

fun printDifferenceDateForHours() {

        val currentTime = Calendar.getInstance().time
        val endDateDay = "03/02/2020 21:00:00"
        val format1 = SimpleDateFormat("dd/MM/yyyy hh:mm:ss",Locale.getDefault())
        val endDate = format1.parse(endDateDay)

        //milliseconds
        var different = endDate.time - currentTime.time
        countDownTimer = object : CountDownTimer(different, 1000) {

            override fun onTick(millisUntilFinished: Long) {
                var diff = millisUntilFinished
                val secondsInMilli: Long = 1000
                val minutesInMilli = secondsInMilli * 60
                val hoursInMilli = minutesInMilli * 60
                val daysInMilli = hoursInMilli * 24

                val elapsedDays = diff / daysInMilli
                diff %= daysInMilli

                val elapsedHours = diff / hoursInMilli
                diff %= hoursInMilli

                val elapsedMinutes = diff / minutesInMilli
                diff %= minutesInMilli

                val elapsedSeconds = diff / secondsInMilli

                txt_timeleft.text = "$elapsedDays days $elapsedHours hs $elapsedMinutes min $elapsedSeconds sec"
            }

            override fun onFinish() {
                txt_timeleft.text = "done!"
            }
        }.start()
    }

I have read your question, I have already faced this problem and I made custom code for it without using any third party library.

Here is Code:

public class CountDownTimerDaysActivity extends AppCompatActivity {

/*Views declaration*/
private TextView months_left,weeks_left,daysLeft,hrsLeft,minLeft,secLeft,endDate;
/*Handler Declaration*/
private Handler handler;
/*set End Time for timer */
private String endDateTime="2019-03-21 10:15:00";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /*inflate layout for activity*/
    setContentView(R.layout.activity_count_down_timer);
    /*invoke initView method  for views*/
    initView();
}
/*initView method for findviews by id*/
private void initView() {
    months_left = findViewById(R.id.months_left);
    weeks_left = findViewById(R.id.weeks_left);
    daysLeft = findViewById(R.id.days_left);
    hrsLeft = findViewById(R.id.hrs_left);
    minLeft = findViewById(R.id.min_left);
    secLeft = findViewById(R.id.sec_left);
    endDate = findViewById(R.id.end_date);
    endDate.setText(endDateTime);
    /*invoke countDownStart() method for start count down*/
    countDownStart();
}

/*countDownStart() method for start count down*/
public void countDownStart() {
    handler = new Handler();
    Runnable runnable = new Runnable() {
        @SuppressLint("SetTextI18n")
        @Override
        public void run() {
            handler.postDelayed(this, 1000);
            try {
                @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                // Please set date in  YYYY-MM-DD hh:mm:ss format
                /*parse endDateTime in future date*/
                Date futureDate = dateFormat.parse(endDateTime);
                Date currentDate = new Date();
                /*if current date is not comes after future date*/
                if (!currentDate.after(futureDate)) {
                    long diff = futureDate.getTime()
                            - currentDate.getTime();

                    long days = diff / (24 * 60 * 60 * 1000);
                    diff -= days *(24  *60 * 60  *1000);
                    long hours = diff / (60 * 60*  1000);
                    diff -= hours * (60*  60 * 1000);
                    long minutes = diff / (60 * 1000);
                    diff -= minutes * (60  *1000);
                    long seconds = diff / 1000;
                    @SuppressLint("DefaultLocale") String dayLeft = "" + String.format("%02d", days);
                    @SuppressLint("DefaultLocale") String hrLeft = "" + String.format("%02d", hours);
                    @SuppressLint("DefaultLocale") String minsLeft = "" + String.format("%02d", minutes);
                    @SuppressLint("DefaultLocale") String secondLeft = "" + String.format("%02d", seconds);
                    daysLeft.setText(dayLeft + "D: ");
                    hrsLeft.setText(hrLeft + "H: ");
                    minLeft.setText(minsLeft + "M: ");
                    secLeft.setText(secondLeft + "S");

                } else {
                    textViewGone();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    handler.postDelayed(runnable, 1000);
}
private void textViewGone() {
    months_left.setVisibility(View.GONE);
    weeks_left.setVisibility(View.GONE);
    daysLeft.setVisibility(View.GONE);
    hrsLeft.setVisibility(View.GONE);
    minLeft.setVisibility(View.GONE);
    secLeft.setVisibility(View.GONE);
}
}

I hope its work for you

Related