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?
The easiest way to modify the value of a LocalDate , LocalTime or LocalDateTime object is to use the with() method of the corresponding object. These methods will return a modified version of the object, it doesn't change the attribute of the original object.
The getHour() method of an LocalDateTime class is used to return the hour-of-day field. This method returns an integer value ranging from 0 to 23, i.e. the Hours of a day.
The easiest way to create an instance of the LocalDateTime class is by using the factory method of(), which accepts year, month, day, hour, minute, and second to create an instance of this class. A shortcut to create an instance of this class is by using atDate() and atTime() method of LocalDate and LocalTime class.
now() now() method of a LocalDateTime class used to obtain the current date-time from the system clock in the default time-zone. This method will return LocalDateTime based on system clock with default time-zone to obtain the current date-time.
Currently you can enable java.time
in lower level apis by upgrading grade to 4.0.0. read more here.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With