DreamsInHD
DreamsInHD

Reputation: 413

Android api 21: parsing ISO8601 duration

I'm trying to show a duration on screen that I get from an api in the ISO8601 format, the format looks like this: PT#H#M#S. I found this solution:

Duration duration = Duration.parse(durationString);

But it requires api level 26, and my min is 21.

Is there a different way?

Upvotes: 0

Views: 1164

Answers (1)

Anonymous
Anonymous

Reputation: 86120

You’ve found the good solution. And the good news is you don’t need to give it up even though you’re coding for API level 21. I suggest you add the ThreeTenABP library to your project. And make sure you import org.threeten.bp.Duration rather than the version from java.time. Then you should be fine.

ThreeTenABP is the Android edition of ThreeTen Backport, which in turn is the backport of java.time, the modern Java date and time API that includes the Duration class, to Java 6 and 7. The backport is created by the same people who developed java.time and is reported rock solid.

I was once told that dependencies for ThreeTenABP is (I didn’t test):

compile group: 'org.threeten', name: 'threetenbp', version: '1.3.3', classifier: 'no-tzdb'

Links

Upvotes: 1

Related Questions