I'm trying to make a countdown in days, hours, minutes, seconds from a starting date and I'm getting the days wrong for some reason I cannot find.
String givenDateString = "2019-05-15T09:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
try {
Date mDate = sdf.parse(givenDateString);
timeInMilliseconds = mDate.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
new CountDownTimer(timeInMilliseconds, 1000) {
@Override
public void onTick(long millisUntilFinished) {
long day = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
millisUntilFinished -= TimeUnit.DAYS.toMillis(day);
long hour = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
millisUntilFinished -= TimeUnit.HOURS.toMillis(hour);
long minute = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
millisUntilFinished -= TimeUnit.MINUTES.toMillis(minute);
long second = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);
prueba.setText("Days: "+day+" Hours: "+hour+" Minutes: "+minute+" Seconds: "+second);
}
@Override
public void onFinish() {
// What ever you want !
}
}.start();
And I'm getting this result:
New error android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
TextView prueba;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prueba = findViewById(R.id.prueba);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date currentDate = null;
Date destinationDate = null;
try {
currentDate = Calendar.getInstance().getTime();
destinationDate = sdf.parse("2019-05-15T09:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
calculateDifference(currentDate, destinationDate);
}
}, 0, 1000);//Update text every second
}
public void calculateDifference(Date startDate, Date endDate) {
long different = endDate.getTime() - startDate.getTime();
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long days = different / daysInMilli;
different = different % daysInMilli;
long hours = different / hoursInMilli;
different = different % hoursInMilli;
long minutes = different / minutesInMilli;
different = different % minutesInMilli;
long seconds = different / secondsInMilli;
Log.e("calculation", "Days: " + days + " Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds);
prueba.setText("Days: " + days + " Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds);
}
Hello there here is how you can achieve this
Here is the method which calculate the difference between two date.
public void calculateDifference(Date startDate, Date endDate) {
long different = endDate.getTime() - startDate.getTime();
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long days = different / daysInMilli;
different = different % daysInMilli;
long hours = different / hoursInMilli;
different = different % hoursInMilli;
long minutes = different / minutesInMilli;
different = different % minutesInMilli;
long seconds = different / secondsInMilli;
Log.e("calculation", "Days: " + days + " Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds);
}
You can settext of you text view instead of log
And this is how you can call the timer with date
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date currentDate = null;
Date destinationDate = null;
try {
currentDate = Calendar.getInstance().getTime();
destinationDate = sdf.parse("2019-05-15T09:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
calculateDifference(currentDate, destinationDate);
}
}, 0, 1000);//Update text every second
}