Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Duration to LocalDateTime for API level lower than 26

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?

like image 773
fpiechowski Avatar asked Feb 21 '18 15:02

fpiechowski


People also ask

How do I change the time on LocalDateTime?

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.

How do I get hours from LocalDateTime?

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.

How can we create LocalDateTime object with current date and time?

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.

Which method of LocalDateTime can be used to get the current date and time?

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.


2 Answers

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

like image 54
Nithin Avatar answered Oct 27 '22 00:10

Nithin


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.

like image 43
Ehsan Heidari Avatar answered Oct 26 '22 22:10

Ehsan Heidari