Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between Java calendar vs Android calendar

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);
like image 661
Islam Assem Avatar asked Jan 02 '23 16:01

Islam Assem


1 Answers

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?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 brought some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android (26+) bundle implementations of the java.time classes.
    • For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
      • If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….

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

like image 141
Basil Bourque Avatar answered Feb 10 '23 22:02

Basil Bourque