Android Countdown Timer to Date

前端 未结 7 1879
死守一世寂寞
死守一世寂寞 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 12:10

    long startTime;

    private void start_countdown_timer()
    {
        SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
        formatter.setLenient(false);
    
    
        String endTime = "18.09.2017, 15:05:36";
        long milliseconds=0;
    
        final CountDownTimer mCountDownTimer;
    
        Date endDate;
        try {
            endDate = formatter.parse(endTime);
            milliseconds = endDate.getTime();
    
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        startTime = System.currentTimeMillis();
    
    
        mCountDownTimer = new CountDownTimer(milliseconds, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
    
                startTime=startTime-1;
                Long serverUptimeSeconds =
                        (millisUntilFinished - startTime) / 1000;
    
                String daysLeft = String.format("%d", serverUptimeSeconds / 86400);
                //txtViewDays.setText(daysLeft);
                Log.d("daysLeft",daysLeft);
    
                String hoursLeft = String.format("%d", (serverUptimeSeconds % 86400) / 3600);
                //txtViewHours.setText(hoursLeft);
                Log.d("hoursLeft",hoursLeft);
    
                String minutesLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) / 60);
                //txtViewMinutes.setText(minutesLeft);
                Log.d("minutesLeft",minutesLeft);
    
                String secondsLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) % 60);
                //txtViewSecond.setText(secondsLeft);
                Log.d("secondsLeft",secondsLeft);
    
    
            }
    
            @Override
            public void onFinish() {
    
            }
        }.start();
    
    
    }
    

提交回复
热议问题