If you’re using Gradle plugin 4.0 or later (with Android Studio 4.0 or later), you can take advantage of D8 Core Library Desugaring. This includes a subset of the functionality found in java.time
and will allow you to use java.time.Instant
in your project; even if you need to support versions older than API 26.
In your module’s build.gradle
file:
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
coreLibraryDesugaringEnabled true
}
// If using Kotlin
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
}
dependencies {
…
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.5'
}
You should now be able to use this class error free.
Some sources: