How do I use the new Java 8 Date API?

Asked

Viewed 181 times

1

I would like to know how I compile in my Gradle Build to make available the new java 8 date API in my android studio

  • 1

    See help: http://stackoverflow.com/a/21212790/2570426

  • 1

    Friend, the question as formulated is currently extremely vague, please read how to ask a proper question and provide more details about your project, what you have already done and your level of knowledge about it. Without it, the most they can provide you with negative and maybe, some links.

  • Dude, the question is really simple and straightforward. Currently I use the style of Date and Calendar dates in android studio, and I just want the 'Compile' to be available the new Java 8 date API. Simple

  • Ack Lay, what I want is to know exactly how to configure my Gradle to be compatible

3 answers

8

Edition 2020/06/01

With Android Studio 4.0 and Android Gradle plugin 4.0.0 support for Java 8 has been extended, allowing you to use more Api’s without requiring a minimum API level.

To enable support those Apis are required to include in the build.Gradle the following:

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.5'
}

List of supported Api’s.


Edition 2019/04/27

To java time. is available from Api 26.
Require compileSdkVersion >= 26 and minSdkVersion >= 26


Edition 2017/11/10

If you’re using Android Studio 3.0 configure your build.Gradle as follows:

android {
  ...
  // Configure only for each module that uses Java 8
  // language features (either in its source code or
  // through dependencies).
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

For more information see Use Java 8 Language Features.


Android does not support all features of Java 8 and most of it can only be used in application development for Android 7(API 24).

Supported features:(in any API level)

  • Default and Static interface methods
  • Lambda Expressions
  • Repeatable Annotations
  • Method References
  • Type Annotations

Reflection and language-related Apis:(API level 24 or higher)

  • java.lang.Functionalinterface
  • java.lang.Annotation.Repeatable
  • java.lang.reflect.Method.isDefault()

Utility Apis:(API level 24 or higher)

  • java.util.Function
  • java.util.stream

As you can check the Java 8 Date API is not included in the supported Apis and therefore cannot use it.

If however you want to use the other features of Java 8 configure your build.Gradle as follows:

android {
  ...
  defaultConfig {
    ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

5


I think that none of the answers meet the OP, since he has an interest in JSR 310, which is the new API for working with dates without having to stress deal with classes Date and Calendar java.

I believe using the Java 8 compatibility provided by deceased Jack does not provide JSR 310.

My suggestion is to go to your ported versions, ie use the Joda Time or the ThreeTen ABP, and I have recommended more the second option.

Follow the documentation from Three Ten ABP and the documentation of Joda Time

You can easily use by adding Gradle dependency:

dependencies {
    compile 'net.danlew:android.joda:2.9.7'
}

or

dependencies {
    compile 'com.jakewharton.threetenabp:threetenabp:1.0.5'
}
  • Thank you very much! It was really this :D

2

To enable the language features of Java 8 and Jack for your project, insert the following code below into the archive build.gradle level of the module. The sourceCompatibility and targetCompatibility. See how it should look:

android {
  ...
  defaultConfig {
    ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

See more details for use of Java 8 language resources in the documentation.

Note: Java 8 features can only be used in Android Studio 2.1 or higher.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.