What is difference between Java calendar vs Android calendar
Asked Answered
A

1

7

I'm new to Android and I'm trying to get the current date but the Android Calendar class requires API 24. I need to support older device and the Time class is deprecated since API 22.

The problem is how to get current date on API 23 and I solved it by using java.util.Calendar which works on all versions. So what should I use, Android calendar or Java calendar?

Note that day, month and year are just integers when using the Android calendar.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    day =android.icu.util.Calendar.getInstance().get(android.icu.util.Calendar.DAY_OF_MONTH);
    month = android.icu.util.Calendar.getInstance().get(android.icu.util.Calendar.MONTH);
    year = android.icu.util.Calendar.getInstance().get(android.icu.util.Calendar.YEAR);
}
else {
    day = java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH);
    month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
    year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
}

And when using only Java calendar there's no need to check the API version

day = java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH);
month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
Auburta answered 23/10, 2018 at 15:7 Comment(0)
C
15

tl;dr

So what should I use, Android calendar or Java calendar?

Neither.

Use the modern java.time classes. Available in all Android versions via Gradle Plugin 4.

LocalDate.of( 2018 , Month.JANUARY , 23 ) 

Avoid legacy classes

In Java, the terrible java.util.Calendar class (and related classes, Date etc.) was supplanted years ago by the java.time classes. Now available in Android 26 and later.

The ThreeTen-Backport library bring must of the java.time functionality to Java 6 & 7 using virtually identical API. That project is further adapted for Android <26 in the ThreeTenABP project.

I strongly recommend avoiding the legacy classes, instead using only the java.time classes. These modern classes are defined in JSR 310, and led by the same man who led the industry-leading Joda-Time library, Stephen Colebourne. So they have excellent design based on many years of experience. Using ThreeTenABP is well worth the bother of adding the library to your project.

For a date-only, year-month-day, without a time-of-day and without a time zone, use the LocalDate class.

LocalDate ld = LocalDate.of( 2018 , 1 , 23 ) ; 

Or use the handy Month enum for more readability.

LocalDate ld = LocalDate.of( 2018 , Month.JANUARY , 23 ) ; 

Table of all date-time types in Java, both modern and legacy


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.

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

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

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Table of which java.time library to use with which version of Java or Android

Christman answered 23/10, 2018 at 18:59 Comment(5)
this require API 26 and my problem how to support old versionsAuburta
@IslamAssem Reread my Answer more carefully looking for “Android” in bullets at the end.Christman
Could you please explain in details why the legacy date-time classes are troublesome ?Blanchblancha
@Blanchblancha What's wrong with Java Date & Time API?. And those Answers there only cover some of the problems, there are more. One of those problems is missing types, unable to represent important date-time concepts. I’ll add a graphic table here to show what is lacking.Christman
May I suggest, mention of ThreeTenABP should be the footnote and desugaring should be front and center as the recommendation. It doesn't make sense to use ThreeTenABP in new projects when desugaring is available. This answer reads like it is strongly recommending ThreeTenABP and it would be easy to read it and miss the comment about desugaring.Brownnose

© 2022 - 2024 — McMap. All rights reserved.