How can I use the new Java 8 Date API?

0

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

    
asked by anonymous 15.03.2017 / 14:30

3 answers

5

I do not think any of the answers meets the OP, given that he is interested in JSR 310, which is the new API for working with dates without having to stress > handle classes Date and Calendar of Java.

I believe using the Java 8 compatibility given by expired Jack does not provide the JSR 310.

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

Follow the documentation for Three Ten ABP and the documentation for Joda Time

You can easily use it by adding the Gradle dependency:

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

or

dependencies {
    compile 'com.jakewharton.threetenabp:threetenabp:1.0.5'
}
    
16.03.2017 / 01:46
6

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

Supported Features:

  • Default and static interface methods
  • Lambda expressions (also available in API level 23 and below)
  • Repeatable annotations
  • Method References (also available in API level 23 and below)
  • Type Annotations (also available in API level 23 and below)

Reflection and APIs related to the language:

  • java.lang.FunctionalInterface
  • java.lang.annotation.Repeatable
  • java.lang.reflect.Method.isDefault ()

Utility APIs:

  • java.util.function
  • java.util.stream

How you can check the Java 8 Date API is not included in the supported APIs and therefore you can not use it.

If you want to use the other features of Java 8, configure build.gradle as follows:

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

Edit 2017/11/10

If you're using Android Studio 3.0, set up 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 .

References:

15.03.2017 / 15:07
2

To enable the Java 8 language features and Jack for your project, insert the following code below into the build.gradle module-level file. The sourceCompatibility and targetCompatibility . Here's how it should look:

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

See use of Java 8 language resources in the documentation.

Note: You can only use Java 8 features in Android Studio 2.1 or higher.     

15.03.2017 / 14:54