

Modern date and time handling in all Android versions (without sugar)
source link: https://www.rockandnull.com/java-time-android/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Working with time in Android has always been tricky. People have been using either third party libraries (e.g.Joda-Time, ThreeTenBp ) or had to work with the built-in time classes (such asCalendar). The built-in classes, although available in all Android versions, had a not-so-great API, were mutable (so not thread-safe), and timezone handling was a pain ( ref ).
In the latest Android Gradle plugin release (v.4.0.0+), " Java 8+ API desugaring support " was introduced. In plain language, we can use some Java 8 APIs and the plugin will translate those APIs to Android-compatible Java behind the scenes that can be used in all Android versions without requiring a minimum API level!
In general, Kotlin provides its Collections and Stream API replacements which are superior to Java's in my opinion. So from the list of Java 8 APIs , I think that only the excellent java.time API is interesting for a Kotlin developer. Finally, a nice date/time API that can be used in Android!
Note that this "desugaring" process might cost a few extra KB in your APK/AAB. But if that's not a deal-breaker for your case, you should consider using the newer APIs.
Set up
You would need to modify your build.gradle
files. More details (and up-to-date version codes) in the official doc
.
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' [...] }
Meet the classes
The time API is quite comprehensive and the complete exploration of the API is outside of the scope of this post. But, I want to introduce what I believe the main classes you probably going to need most of the time.
Instant
Instant is a point in time. Can be considered equivalent to a timestamp, if you are familiar with that term.
LocalTime, LocalDate, LocalDateTime
Each class represents the time, date, date+time respectively, without a timezone. Most people when thinking about date/time think in these terms anyway.
ZoneOffset, ZoneId
These are used to work with timezones. You would need one of those to convert a Local*
to an Instant
.
ZonedDateTime
You probably guessed it. This is what you get when combine a LocalDateTime
with a ZoneId
. This can be converted to an Instant
.
Playing with date and time
These APIs provide intuitive and consistent ways to manipulate date and time. Remember that all objects are immutable, so a new object is created every time that a manipulation takes place (which is a good thing). Some useful examples follow.
val someDate = LocalDate.of(2020, 7, 28) val someDateTime = LocalDateTime.ofEpochSecond(1595363833) val someInstant = Instant.parse("2007-12-03T10:15:30.00Z")
val today: LocalDate = LocalDate.now() val startOfDay: LocalDateTime = today.atStartOfDay()
LocalDate
val now: LocalDateTime = LocalDateTime.now() val nowInLA: ZonedDateTime = now.atZone(ZoneId.of("America/Los_Angeles"))
val instant: Instant = nowInLA.toInstant() val timestampInSeconds: Long = instant.epochSecond val timestampInMilliseconds: Long = instant.toEpochMilli()
val today: LocalDate = LocalDate.now() val previousWeek: LocalDate = LocalDate.now().minusWeeks(1)
Formatting
Just note that the formatters you are used to in Android, such as SimpleDateFormat
, cannot be used with Java Time API. The API comes with its own formatters, such as DateTimeFormatter
.
DateTimeFormatter.ISO_DATE.format(instant)
Enjoy a modern date/time API in your Android development life :)
Recommend
-
27
Image from pexels Working with dates is a p...
-
51
README.md Chef Sugar
-
9
Hollow to solid or spun sugar development I was recently discussing “hollow to solid” product development with a cadre of senior engineers. A quick search didn’t yield any articles about this, as I wouldn’t...
-
6
Block sugar in expressions Dec 29, 2011 UPDATE: I found some more complications. Updates inline. I have been working on and off on allowing block sugar to appear in Rust expressions and not only...
-
11
Handling Different Kafka Message VersionsI was in a job interview a while ago and one of the problems raised there was handling different message ve...
-
11
Date handling, databases, and ramrods Readers who are familiar with my older posts (whether directly, or through the book) might remember my
-
2
Handling Effective Date and Expiry Date with DMN DecisionsDMN is a modeling language and notation for the precise specificatio...
-
5
Alain Perkaz Follow A passionate and disciplined software engineer. Handling date strings in TypeScript April 4, 2022 3 min r...
-
6
Handling of Versions of TechZone Toolkit Modules With the TechZone Accelerator Toolkit IBM software, open source projects and custom applications can easily be deployed to various clouds. Th...
-
4
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK