Wrangling Dates & Time in Android

Dec 15 2022 · Kotlin 1.6.21, Android 13, IntelliJ 2022.1

Part 1: Wrangling Dates & Time in Android

09. Implement the Data-Time API in Android

Episode complete

About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 08. Work the Kotlinx-Datetime API

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 09. Implement the Data-Time API in Android

Congrats! You’ve finished the course about the Java Date-Time library. But you still miss one piece of the puzzle, implementing it on Android. It might sound very easy to do so, but there’s something you need to know. Java Date-Time library comes out with Java 8. Android, however, natively supports Java 8 only since Android ******SDK 26, also known as Android Oreo. So how can we support version prior to the SDK 26. Well, fortunately, Google has released the Core Library Desugaring, which implements the new language features by performing bytecode transformations called desugar. If you reach minSDK 26 in the future you can simply remove the library desugaring to use the build-in Java 8 APIs.

Here’s the list of supported APIs:

  • Sequential streams (java.util.stream)
  • A subset of java.time
  • java.util.function
  • Recent additions to java.util.{Map,Collection,Comparator}
  • Optionals (java.util.Optionaljava.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 AtomicIntegerAtomicLong and AtomicReference)
  • ConcurrentHashMap (with bug fixes for Android 5.0)

Now that you know why we need this library for, let’s implement it.

Once you have Android Studio running, open the app Gradle file. This first step is required only if you want to support version 20 or lower of the Android SDK. Version 20 if the SDK corresponds to Android KitKat. So if your minSdkVersion is version 20 or lower, go inside defaultConfig, and add:

// Required when setting minSdkVersion to 20 or lower
multiDexEnabled true

All the following steps are mandatory to make to app work correctly. Inside compileOptions we need to add:

// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled true

Please check if you already have the compatibility for Java 8:

sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8

Usually, Android Studio implements these two lines for you, but if you don’t see them, add them. The last thing we need to do is implementing the desugar dependency. So go inside dependencies and add this line of code:

coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.0'

Alright! Pretty simple, wasn’t it? Now we can use the Data-Time API everywhere inside our project. Let’s check it out.

Open the MainActivity, and now let’s change the "Hello $name!" text with the current date. We can delete the Greeting and DefaultPreview composable since we’re not going to use them. Inside MyApplicationTheme let’s say:

val currentDate = remember {
  LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE)
}

Of course, you should use the ViewModel, or business domain, to gather this kind of information in real-life apps, but it’s not the goal of this example. Now inside Surface we can define a Text object with the currentDate as text. So:

Text(currentDate)

Let’s run the app! As you can see, we printed today’s date. Thanks to the Desugar Library, you can use all the classes you’ve learned inside this course, and much more, to create awesome apps. So… happy coding!