Reputation: 61
I am new to Android I need some help. I am using countdown timmer to update a text view up to some date. Its working fine but the timmer does not stop at 0:0:0:0 it keeps going on as this 0:0:0:-12.
I want the text view text to change to Start at 0:0:0:0.
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss");
formatter.setLenient(false);
String endTime = "22.05.2019, 23:48:00";
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;
countDownTimer = new CountDownTimer(milliseconds, 1000) {
@Override
public void onTick(long millisUntilFinished) {
startTime = startTime - 1;
Long serverUptimeSeconds =
(millisUntilFinished - startTime) / 1000;
Toast.makeText(MainActivity.this, "Time left :" + serverUptimeSeconds, Toast.LENGTH_SHORT).show();
String daysLeft = String.format("%2d", serverUptimeSeconds / 86400);
String hoursLeft = String.format("%2d", (serverUptimeSeconds % 86400) / 3600);
String minutesLeft = String.format("%2d", ((serverUptimeSeconds % 86400) % 3600) / 60);
String secondsLeft = String.format("%2d", ((serverUptimeSeconds % 86400) % 3600) % 60);
countdownTimerText.setText(daysLeft + ":" + hoursLeft + ":" + minutesLeft + ":" + secondsLeft);
}
@Override
public void onFinish() {
countdownTimerText.setText("Start");
}
}.start();
Upvotes: 1
Views: 45
Reputation: 76486
CountdownTimers API takes millisInTheFuture
and an interval
https://developer.android.com/reference/android/os/CountDownTimer.html#CountDownTimer(long,%20long)
much easier to do something like
new CountdownTimer(TimeUnit.Seconds.toMillis(30), TimeUnit.Seconds.toMillis(1))
//^ this will countdown 30 seconds in interval of 1 second
You do basically this:
String endTime = "22.05.2019, 23:48:00";
milliseconds = endDate.getTime(); // 1558565280
new CountdownTimer(milliseconds, 1000)
which will countdown from a very big number :-) (1558565280 millis == 432.93 hours)
Perhaps you meant to do:
countDownTimer = new CountDownTimer(diff, 1000)
Upvotes: 1