10

I am developing an application for API level 19 (KitKat).

I have a LocalDateTime object and a Duration object. I need to add this Duration to LocalDateTime.

Android Studio shows that plus(TemporalAmount) method of LocalDateTime class is avaliable since API level 26 which is Oreo and it supports less than 1% of devices right now.

How can I do it with API level 19?

bbb
  • 5
  • 4
fpiechowski
  • 517
  • 1
  • 7
  • 21
  • The whole `LocalDateTime` class should only be there for API 26, so you need to use another means of time anyway for API 19. – deekay Feb 21 '18 at 16:10
  • 1
    What do you recommend deekay ? – fpiechowski Feb 21 '18 at 16:27
  • 5
    You can use the ThreeTen Backport: http://www.threeten.org/threetenbp/ You'll also need ThreeTenABP: https://github.com/JakeWharton/ThreeTenABP And there's a tutorial here: https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project But you'll need to change your imports to use the backport's classes (from `java.time` to `org.threeten.bp`). – bbb Feb 21 '18 at 16:59

2 Answers2

8

Currently you can enable java.time in lower level apis by upgrading grade to 4.0.0. read more here.

Nithin
  • 5,470
  • 37
  • 44
3

For Android Gradle Plugin 4.0.0+:

Use Java Desugaring feature so you can use Java 8 Date library without any problem for API level lower than 26 too. For more details read this

Old answer:

The bbb answer in the comments is correct.

Currently the best option for android is ThreeTenABP. Jake Wharton explains why it's better to use this library rather than Joda-Time and ThreeTenBP in Android.

Ehsan Heidari
  • 486
  • 6
  • 17