简体   繁体   English

我应该在Android的Date中使用哪个类?

[英]What class should I use for Date's in Android?

I'm currently making an app for android, but I'm unsure which class is actually best to use. 我目前正在为Android制作应用程序,但我不确定哪个类实际上最好用。

What I need to do with the dates that I'm working with is things such as comparing the dates, getting the difference between them, and date calculations (such as adding/subtracting a small period of time such as two weeks or a month to any given date). 我需要处理的是与我合作的日期,例如比较日期,获取它们之间的差异以及日期计算(例如添加/减去一小段时间,例如两周或一个月)任何给定的日期)。

I know that the standard Date class in the Java API is horrible, so I'm just wondering which classes are recommended? 我知道Java API中的标准Date类很糟糕,所以我只是想知道推荐哪些类?

I've seen the GregorianCalendar class, and the Joda Time library. 我见过GregorianCalendar类和Joda Time库。 However, I read on a post that due to the size of the Joda Time library, it wasn't recommended for apps. 但是,我在帖子中读到,由于Joda Time库的大小,不建议用于应用程序。

Any help would be much appreciated. 任何帮助将非常感激。 ^^ ^^

Edit: Upon a comment from 404notfound, I decided to go with the Calendar interface. 编辑:根据404notfound的评论,我决定使用Calendar界面。 This is due to the Calendar interface automatically using the best implementation of it based on the locale and time zone of the user's device. 这是由于Calendar界面根据用户设备的区域设置和时区自动使用它的最佳实现。 This would make it more flexible over its implementation of the GregorianCalendar (or other specific implementations) in terms of places you can roll it out to. 这将使其在实现GregorianCalendar(或其他特定实现)方面更灵活,就可以将其推广到的地方而言。

Hope this is helpful to anyone else with the same conundrum! 希望这对任何有同样难题的人都有帮助!

Edit #2: After further look into the link provided by Nguyễn Hoài Nam, I've went with the ThreeTenABP library, as it's a wrapper for the new Time class featured in Java 8, and will prove to be more fluent in use than a Calendar approach. 编辑#2:在进一步查看NguyễnHoàiNam提供的链接后,我选择了ThreeTenABP库,因为它是Java 8中新的Time类的包装器,并且将证明其使用比使用更流畅。日历方法。

I would recommend looking at the ThreeTenABP library as a better alternative (as that's what I've migrated a similar application to use). 我建议将ThreeTenABP库作为更好的替代方案(因为我已经迁移了类似的应用程序)。 This library is a backport for Android of ThreeTenBP, which in turn is a backport for Java 6 of the latest Java 8's Date Time API (same creator). 该库是Android的ThreeTenBP的后端,后者又是最新Java 8的Date Time API(同一创建者)的Java 6的后端。 (I would encourage you to review the Java 8 Date Time API pros/cons out there first.) Note that ThreeTenBP provides some similar verbs with Joda Time so you won't have hard time migrating them. (我建议您首先查看Java 8 Date Time API的优缺点。)请注意,ThreeTenBP提供了一些与Joda Time类似的动词,因此您不会很难迁移它们。 Last, as noticed in this lib, method count, lib size etc is always a problem in Android. 最后,正如本文中所注意到的,方法计数,lib大小等在Android中始终是一个问题。 This lib solve that. 这个lib解决了这个。

Edited to add a usage guide. 编辑添加使用指南。

  • Add this lib to your build.gradle 将此lib添加到build.gradle

    compile 'com.jakewharton.threetenabp:threetenabp:1.0.1'

  • Create your Application class if you didn't. 如果没有,请创建Application类。 For example, your package is: com.example.app, place a new file named MyApplication.java under that package. 例如,您的包是:com.example.app,在该包下放置一个名为MyApplication.java的新文件。 Snippet here MyApplication . Snippet在这里MyApplication

  • Update your AndroidManifest.xml <application> tag: add this line right under <application : android:name=".MyApplication" 更新AndroidManifest.xml <application>标签:在<applicationandroid:name=".MyApplication"下添加此行android:name=".MyApplication"

  • Run your app. 运行你的应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM