Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OffsetDateTime.now() requires API 26

Tags:

java

android

I need to use method setStartTimestamp(OffsetDateTime startTimestamp) but my minSdk is 21 so it shows me Call requires API level 26. Is there any way how can I use OffsetDateTime on lower apis?

like image 969
Martin Avatar asked Mar 18 '18 14:03

Martin


2 Answers

You can use Java 8+ API desugaring support (Android Gradle Plugin 4.0.0+)

It is described in this link

Simply you have to modify your build.gradle file in your app module like this:

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
  }
}

dependencies {
  coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
}
like image 90
M.Kasaei Avatar answered Nov 03 '22 14:11

M.Kasaei


java.time APIs on Android require API 26.

For older API levels you can use ThreeTenABP which is the Android version of JSR-310 java.time backport for Java 6.

like image 37
laalto Avatar answered Nov 03 '22 14:11

laalto