[英]What is the Android equivalent of java.time.Duration?
I need to use something like the class java.time.Duration
to express time intervals from sub-microsecond to seconds.我需要使用类
java.time.Duration
来表示从亚微秒到秒的时间间隔。 But Android Studio doesn't offer me this option.但是 Android Studio 没有为我提供这个选项。
I suppose I could use a float
and express everything as seconds, but this seems less intuitive and meaningful.我想我可以使用
float
并将所有内容表示为秒,但这似乎不太直观和有意义。
These intervals aren't associated with any real-world timeline, they're just, well, intervals.这些间隔与任何现实世界的时间线无关,它们只是间隔。
See the back-port of much of java.time to Java 6 & 7, and its adaptation for Android .查看大部分 java.time到 Java 6 和 7 的后向移植,以及它对Android 的改编。 See details below.
请参阅下面的详细信息。
The back-port includes the org.threeten.bp.Duration
class.后向端口包括
org.threeten.bp.Duration
类。
The java.time framework is built into Java 8 and later. java.time框架内置于 Java 8 及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
, Calendar
, & SimpleDateFormat
.这些类取代麻烦的老传统日期时间类,如
java.util.Date
, Calendar
,和SimpleDateFormat
。
The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial .要了解更多信息,请参阅Oracle 教程。 And search Stack Overflow for many examples and explanations.
并在 Stack Overflow 上搜索许多示例和解释。 Specification is JSR 310 .
规范是JSR 310 。
Where to obtain the java.time classes?从哪里获得 java.time 类?
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.time
library.您可能会发现导入
JodaTime
库并使用其中的持续时间类可能会有所帮助,因为您无权访问java.time
库。 From what I can gather the Interval and ReadableDuration classes may be useful, but neither explicitly provide a getSeconds()
method.据我所知, Interval和ReadableDuration类可能很有用,但都没有明确提供
getSeconds()
方法。
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())
As is noted at ThreeTenABP you should use desugaring if your gradle plugin is v4.0截至指出ThreeTenABP你应该使用脱糖,如果你的gradle这个插件V4.0
Here you can find how to turn on desugaring: android library desugaring在这里你可以找到如何打开脱糖: android library desugaring
It's pretty simple.这很简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.