Android Countdown Timer to Date

前端 未结 7 1856
死守一世寂寞
死守一世寂寞 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:11

    Try this one:

    SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
            formatter.setLenient(false);
    
    
            String endTime = "25.06.2017, 15:05:36"
    
            Date endDate;
            try {
                endDate = formatter.parse(endTime);
                milliseconds = endDate.getTime();
    
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
             startTime = System.currentTimeMillis();
    
             diff = milliseconds - startTime;
    
    
               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);
    
                    String hoursLeft = String.format("%d", (serverUptimeSeconds % 86400) / 3600);
                    txtViewHours.setText(hoursLeft);
    
                    String minutesLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) / 60);
    
                    txtViewMinutes.setText(minutesLeft);
    
                    String secondsLeft = String.format("%d", ((serverUptimeSeconds % 86400) % 3600) % 60);
                    txtViewSecond.setText(secondsLeft);
    
    
                }
    
                @Override
                public void onFinish() {
    
                }
            }.start();
    
        }
    

提交回复
热议问题