4

I need to use something like the class java.time.Duration to express time intervals from sub-microsecond to seconds. But Android Studio doesn't offer me this option.

I suppose I could use a float and express everything as seconds, but this seems less intuitive and meaningful.

These intervals aren't associated with any real-world timeline, they're just, well, intervals.

Robert Lewis
  • 1,847
  • 2
  • 18
  • 43
  • 1
    Maybe `javax.xml.datatype.Duration`? Or just import JodaTime – OneCricketeer Feb 26 '17 at 19:49
  • The `java.time` libraries only come into effect in Java 8 anyway, and the last I checked, Android can only use up to Java 7. You may be able to get this effect with a third-party library like Joda Time. – Makoto Feb 26 '17 at 19:49
  • @Makoto Might want to update your knowledge :) https://developer.android.com/guide/platform/j8-jack.html – OneCricketeer Feb 26 '17 at 19:50
  • 1
    @cricket_007: Okay, so a *subset*. But not the subset that the OP cares about, nor the subset that I was curious about. – Makoto Feb 26 '17 at 19:51
  • @cricket_007 & Makoto – The [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), and advises migration to [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. Much of the java.time functionality is back-ported to Java 6 & 7 in [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/), further adapted to Android in [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP). This back-port includes `Duration`. See [my Answer](http://stackoverflow.com/a/42478897/642706). – Basil Bourque Feb 27 '17 at 06:11

3 Answers3

6

Back-port

See the back-port of much of java.time to Java 6 & 7, and its adaptation for Android. See details below.

The back-port includes the org.threeten.bp.Duration class.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    Thanks for the clarification ;) You probably get tired of repeatedly answering these "simple" datetime questions with the same format. – OneCricketeer Feb 27 '17 at 06:16
0

You may find that importing the JodaTime library and use the duration class from that could be helpful since you do not have access to the java.timelibrary. From what I can gather the Interval and ReadableDuration classes may be useful, but neither explicitly provide a getSeconds() method.

If you can use the above classes to store your time duration you could possibly create a static class to convert milliseconds into a seconds in a more intuitive way.

float durationInSeconds MyTime.milliToSeconds(myDuration.getMillis())
  • The [Joda-Time](http://www.joda.org/joda-time/) project, now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), advises migration to the [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Feb 27 '17 at 06:04
0

As is noted at ThreeTenABP you should use desugaring if your gradle plugin is v4.0

Here you can find how to turn on desugaring: android library desugaring

It's pretty simple.

acmpo6ou
  • 840
  • 1
  • 12
  • 21