Which class should be used for date in Android?

I am currently creating an Android app, but I'm not sure which class is best used.

What do I need to do with the dates I'm working with, such things as comparing dates, getting the difference between them, and calculating by date (for example, adding / subtracting a short period of time, for example, two weeks or a month before any date).

I know that the standard Date class in the Java API is terrible, so I'm just wondering which classes are recommended?

I saw the GregorianCalendar class and the Joda time library. However, I read a post stating that, due to the size of the Joda Time library, it is not recommended for applications.

Any help would be greatly appreciated. ^^

Edit: After a comment from 404notfound, I decided to go with the Calendar interface. This is connected to the Calendar interface automatically, using the best implementation based on the locale and time zone of the user device. This would make it more flexible than its GregorianCalendar implementation (or other specific implementations) in terms of the places where you can run it.

Hope this helps someone else with the same riddle!

Edit # 2: After further exploring the link provided by Nguyễn Hoài Nam, I went with the ThreeTenABP library, as it is a wrapper for the new Time class, introduced in Java 8, and will be freer to use than the calendar approach.

+7
java android date
source share
1 answer

I would recommend considering the ThreeTenABP library as a better alternative (like the fact that I ported a similar application to use). This library is a backport for Android from ThreeTenBP, which in turn is a backport for Java 6 from the newest Java 8 Date Time API (same creator). (I would advise you first to look at the advantages / disadvantages of the Java 8 Date Time API.) Note that ThreeTenBP provides some similar verbs with Joda Time, so it will not be easy for you to transfer them. The latter, as seen in this lib, count method, lib size, etc., is always a problem in Android. This lib solves this.

Edited to add a user guide.

  • Add this lib to your build.gradle

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

  • Build your application class if you haven’t. For example, your package: com.example.app, place a new file called MyApplication.java under this package. Snippet here is MyApplication .

  • Update the AndroidManifest.xml <application> : add this line directly under <application : android:name=".MyApplication"

  • Launch the app.

+8
source share

All Articles