Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android N Java8 java.time

Tags:

I updated to the latest Android N sdk. The only thing I don't understand is why I cannot import java.time into my code? I thought Java8 is available through Android N. Then why didn't Google add java.time package?

like image 965
IgorGanapolsky Avatar asked Mar 15 '16 01:03

IgorGanapolsky


People also ask

Does Android use Java 8?

Java 8 has been supported natively since Android SDK 26. If you wish to use Java 8 language features and your minimal SDK version is lower than 26, . class files produced by the javac compiler need to be converted to bytecode that is supported by these SDK versions.

Which class can get date and time in Java 8?

Using LocalDate, LocalTime and LocalDateTime. The most commonly used classes are LocalDate, LocalTime and LocalDateTime. As their names indicate, they represent the local date/time from the context of the observer.

Is Android Java different from Java?

What is the difference between Android and Java? Java is a programming language, while Android is a mobile phone platform. Android development is java-based (most of the times), because a large portion of Java libraries is supported in Android.

What is a LocalDate in the Java 8 date and time API?

A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03 . LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day. Other date fields, such as day-of-year, day-of-week and week-of-year, can also be accessed.


2 Answers

java.time package was added only in API 26 (Android O): https://developer.android.com/reference/java/time/package-summary.html

UPDATE

But, starting with version 4.0 Android Studio allows using a subset of java.time API (along with a number of others Java 8 language APIs), without requiring a minimum API level for your app:
https://developer.android.com/studio/preview/features#j8-desugar

The following set of APIs is supported in this release:

  • Sequential streams (java.util.stream)
  • A subset of java.time
  • java.util.function
  • Recent additions to java.util.{Map,Collection,Comparator}
  • Optionals (java.util.Optional, java.util.OptionalInt and java.util.OptionalDouble) and some other new classes useful with the above APIs
  • Some additions to java.util.concurrent.atomic (new methods on AtomicInteger, AtomicLong and AtomicReference)
  • ConcurrentHashMap (with bug fixes for Android 5.0)

To enable support for these language APIs, one needs to include the following lines build.gradle file:

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.4' } 
like image 124
Idolon Avatar answered Oct 10 '22 19:10

Idolon


Android N is not supporting all the features of Java 8. Following features are only supported:

  • Default and static interface methods
  • Lambda expressions
  • Repeatable annotations

Reflection and language-related APIs:

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

and Reflection APIs associated with repeatable annotations, such as AnnotatedElement.getAnnotationsByType(Class)

Utility APIs:

  • java.util.function

For more info check the following link: http://developer.android.com/preview/j8-jack.html

like image 33
Maheshwar Ligade Avatar answered Oct 10 '22 19:10

Maheshwar Ligade