I'm porting an iOS app to Android, target API 22. I do a bunch of Date/TimeInterval stuff. I do it all in UTC so it just works for me.
I'm struggling with what to use for Android. There's Date (from the beginning of time, ha ha). I guess I could do duration math by converting to/from the time
milliseconds attribute. It's not clear to me what zone/rules apply to those.
I see people who seem to use Calendars as Date objects and pass those around. That seems a bit off.
And then there's the new Java 8 java.time stuff which looks pretty complete actually (ZonedDateTime, Duration, etc). But that's API 26 and newer. Is there a back port of this that's reliable?
My needs are pretty basic (I think):
grab "now" time (in UTC)
be able to subtract two times and get a time delta
be able to subtract a time delta from a time to get a new time
be able to add a time delta to a time to get a new time
be able to display/stringify times in the current/local time zone
... and as a bonus
easily instantiate given UTC times from something like ISO8601 strings, for unit testing purposes
Yes, there is a backport of the “Java 8 stuff”, also known as java.time or just the modern Java date and time API. The backport has even been adapted specifically for Android.
org.threeten.bp
with subpackages.I am told that dependencies is:
compile group: 'org.threeten', name: 'threetenbp', version: '1.3.3', classifier: 'no-tzdb'
I am sorry that I haven’t got experience with ThreeTenABP myself. My experience with java.time suggests that it is just what you need. The backport was developed by the same folks that developed java.time, so I would immediately suppose that it’s as reliable as it gets. Only limitation seems to be that it is not getting upgraded with the (minor) new functionality added to java.time in Java 9 and later.
java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).