cannot resolve symbol 'java.time.LocalDate' error in android studio
Asked Answered
M

4

20

I am trying to use java.time.LocalDate in my android app, however if I try to import it, then any class from java.time gives a cannot resolve symbol error in Android studio 1.1

The only reason I could figure out for this is that it doesn't support java 8. Is that really the case?

Mencius answered 26/2, 2015 at 14:37 Comment(1)
That other Question is not really a duplicate. It asks about Java 8 in general, while this Question here focuses on a specific package of classes. And now in 2016, you can sort of use Java 8 language features in Android development but not use the java.time classes. So two different problems, two different solutions, and two different Questions.Burglarious
B
19

Android API level 26

Android API level 26 gained an implementation of java.time including your LocalDate class.

Earlier Android

For Android <26, alternatives include:

  • ThreeTen-Backport is a back-port of much of the java.time functionality to Java 6 & 7.
  • Joda-Time is commonly used in Android projects. Joda-Time inspired the java.time package in Java 8, but they are not drop-in replacements. The Joda-Time team advises migration to java.time.

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.

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

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

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.

Where to obtain the java.time classes?

Burglarious answered 26/2, 2015 at 15:50 Comment(4)
+1 for the hint that Threeten-backport is not a complete backport (i18n-support is different due to changed environment and also less calendar support).Cocky
java.time was added in API26 developer.android.com/reference/java/time/LocalDate.htmlFurman
@Furman So noted in my revised Answer. ThanksBurglarious
I find it interesting that Android Studio does not warn of this?Toffeenosed
C
5

Yes, Android does not support Java-8. And about the new date/time-API contained in Java-8, you can look at this issue:

http://code.google.com/p/android/issues/detail?id=62192

Currently there are no plans to introduce JSR-310 (java.time-package) in Google Android.

Cocky answered 26/2, 2015 at 14:45 Comment(0)
S
2

Finally date/time-API contained in Java 8 is added to Android API level 26 which was recently released.

DateTimeFormatter API 26

Shitty answered 24/8, 2017 at 18:13 Comment(1)
but useless because it only support if your current min SDK is 26Gide
G
1

You can now use java.time APIs and other Java 8+ APIs without requiring a minimum API level for your app thank to API desugaring support (Android Gradle Plugin 4.0.0+)

https://developer.android.com/studio/write/java8-support#library-desugaring

Briefly, you have to use Android Gradle plugin 4.0.0 or higher and include the coreLibraryDesugaringEnabled true in your compileOptions

android {
  defaultConfig {
    // Required when setting minSdkVersion to 20 or lower
    multiDexEnabled true
  }

  compileOptions {
    // Flag to enable support for the new language APIs
    coreLibraryDesugaringEnabled true
    // Sets Java compatibility to Java 8
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}
Gaskill answered 5/3, 2021 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.