-4

I need an alternative solution for ChronoUnit.Days.between() that works for Android version prior to API 26.

Please!

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Ikemefuna Nwodo
  • 101
  • 1
  • 2
  • 7

2 Answers2

2

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • 3
    Normally I would not upbvote a link only answer, but this one is to the point and does fully answer the question. The question isn’t specific enough to warrant more depth in the answer. – Ole V.V. Feb 04 '19 at 13:07
0

You could use TimeUnit instead (since Java 5):

long diffEpochMillis = Math.abs(lastDate.getTime() - firstDate.getTime());
long diffDays = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);

João Dias Amaro
  • 511
  • 6
  • 14