I need an alternative solution for ChronoUnit.Days.between() that works for Android version prior to API 26.
Please!
I need an alternative solution for ChronoUnit.Days.between() that works for Android version prior to API 26.
Please!
You can use ThreeTen Android Backport
.
The ChronoUnit
enum that you are referring to is part of java.time (the modern Java date and time API). This has been backported, and the backport further adapted for Android. So you may in most cases keep the code you wrote for API level 26 and only modify your imports to import org.threeten.bp.temporal.ChronoUnit
, etc.
You could use TimeUnit
instead (since Java 5):
long diffEpochMillis = Math.abs(lastDate.getTime() - firstDate.getTime());
long diffDays = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);