I make an Application where I need to obtain the remaining time for the day of an event, using the class CountDownTimer
I managed to obtain a counter of days, hours and dates but I can't think of how to obtain the remaining time.
For this I have two dates established by means of two Date
and I need to know the time remaining between them, for the date of the event I have the format:
Date(int year, int month, int day, int hour, int minute, int second);
And for the current time I just generate a new date
Date date = new Date();
How can I get the remaining time in days, hours, minutes and seconds.
My code where I only have the counter with the current time until the event:
First declare the variables:
private TextView countDown;
private CountDownTimer countDownTimer;
private long initialTime =
DateUtils.DAY_IN_MILLIS * 110 +
DateUtils.HOUR_IN_MILLIS * 9 +
DateUtils.MINUTE_IN_MILLIS * 3 +
DateUtils.SECOND_IN_MILLIS * 42;
and then handle it in the CountDownTimer
:
countDownTimer = new CountDownTimer(initialTime, 1000) {
StringBuilder time = new StringBuilder();
@Override
public void onTick(long millisUntilFinished) {
time.setLength(0);
// Use days if appropriate
if(millisUntilFinished > DateUtils.DAY_IN_MILLIS) {
long count = millisUntilFinished / DateUtils.DAY_IN_MILLIS;
if(count > 1)
time.append(count).append(" Dias ");
else
time.append(count).append(" Dia ");
millisUntilFinished %= DateUtils.DAY_IN_MILLIS;
}
time.append(DateUtils.formatElapsedTime(Math.round(millisUntilFinished / 1000d)));
countDown.setText(time.toString());
}
@Override
public void onFinish() {
countDown.setText(DateUtils.formatElapsedTime(0));
countDown.setText("Dia del evento");
}
}.start();
First you have to get the difference in milliseconds:
Then do the operations to know the difference in the unit you require:
You must consider that when doing the integer division, a truncation error is introduced into the calculation.
With this method you can achieve it and it is similar to what you are doing:
We determine the dates on which we want to obtain the difference:
From the values we execute the method
getDiferencia()
with the initial and final date:The method internally prints the start and end date, so you can check the values:
and obtains a string with the values of the difference:
I add my complete solution for my problem for future questions.
First declare the variables to use.
We then initialize the variable
calendar
with a new instance and send the date of our future event.Now we initialize the type variables
Date
that areeventDate
= event day andpresentDate
= current day, the first gets its value from the previously created calendar and the second from the current day.Next step we obtain the difference in milliseconds assigning first the day of the event and then the current day, we save the results in a type variable
long
and send them toLog
to verify that the result is not negative, if so the dates are wrongly assigned.Next step we obtain the equivalence in days, hours, minutes and seconds with these simple mathematical operations, all the results are stored in type variables
long
.This will give us the following result, for example:
Since I need to make a counter towards the date, I initialize the variable
ìnitialTime
with the value of the differencediff
.And then I assign the values to the constructor to initialize the variable
countDownTimer
:With this the
CountDownTimer
works from the current date to the event date.