Android Countdown Timer to Date

前端 未结 7 1868
死守一世寂寞
死守一世寂寞 2020-12-14 11:34

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 fin

7条回答
  •  遥遥无期
    2020-12-14 12:16

    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

提交回复
热议问题