Droid API 22+选项的日期/持续时间(在Kotlin中)? [英] Droid API 22+ options for Date/Duration (in Kotlin)?

查看:105
本文介绍了Droid API 22+选项的日期/持续时间(在Kotlin中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将iOS应用程序移植到Android,目标API是22.我做了很多有关Date/TimeInterval的工作.我使用UTC进行所有操作,因此对我有用.

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.

我在为Android使用什么方面苦苦挣扎.有日期(从时间开始,哈哈).我想我可以通过转换为time毫秒属性来进行持续时间数学运算.我不清楚适用于哪些区域/规则.

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.

然后有新的Java 8 java.time东西,实际上看起来很完整(ZonedDateTime,Duration等).但这是API 26及更高版本.有可靠的后端口吗?

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):

  • 抓住现在";时间(以UTC为单位)

  • 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

...并作为奖励

可以轻松地从ISO8601字符串等实例化给定的UTC时间,以进行单元测试

easily instantiate given UTC times from something like ISO8601 strings, for unit testing purposes

推荐答案

是的,有一个"Java 8程序"的反向端口,也称为 java.time 或仅是现代Java日期.和时间API.反向端口甚至专门针对Android进行了修改.

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.

  • 对于非Android Java 6和7,请获取ThreeTen反向端口,即新类的反向端口(用于JSR 310的ThreeTen;请参见底部的链接).
  • 对于22级的Android API(以及26以下的任何版本),请使用Android版本的ThreeTen Backport.称为ThreeTenABP.并确保使用子包从org.threeten.bp导入日期和时间类.
  • For non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • For Android API level 22 (and anything under 26) use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

我被告知依赖项是:

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

很抱歉,我自己没有使用ThreeTenABP的经验.我对java.time的经验表明,这正是您所需要的. backport是由开发 java.time 的人开发的,因此我立即假设它是可靠的.唯一的限制似乎是它没有通过Java 9及更高版本中添加到java.time的(次要)新功能进行升级.

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.

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.timeto Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
  • Comment from Satyajit Tarafdar about dependencies

这篇关于Droid API 22+选项的日期/持续时间(在Kotlin中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆