Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Journey Of Time

Journey Of Time

Subhrajyoti Sen

January 19, 2021
Tweet

More Decks by Subhrajyoti Sen

Other Decks in Programming

Transcript

  1. java.util.Date Date is not a date A Date in an

    instance in Time Date = Date + Time
  2. java.util.Date val date = Date(2021, 1, 19) Is this January

    19, 2021? - No Year is 1900-based offset
  3. java.util.Date val date = Date(2021, 1, 19) Is this January

    19, 2021? - No Year is 1900-based offset January is month Zero
  4. java.util.Date val date = Date(2020, 0, 35) print(date.toString()) Expected -

    Exception. What is even January 35? Actual - Wed Feb 04 00:00:00 IST 3920
  5. Joda-Time • De-facto solution before Java 8 • Separate classes

    for each concept • Immutable • Handy classes for arithmetic operations
  6. Joda-Time Separate classes for each concept • LocalTime - Time

    without date • LocalDate - Date without time • Instant - Instance in time • Interval - Interval between two Instants • Duration - Time between Instants in milliseconds
  7. Joda-Time Arithmetic operations and immutability val today = DateTime() val

    aMonthLayer = today.plusMonths(1) val daysBetween = Days.daysBetween(today, aMonthLayer).days val dayOfWeek = today.dayOfWeek val dayOfMonth = today.dayOfMonth
  8. Joda-Time Android • Joda-Time JAR ships with the TimeZone database

    • ClassLoader.getResourceAsStream() is used to load the TZ data and is not memory efficient https://github.com/dlew/joda-time-android
  9. Joda-Time Android • Joda-Time JAR ships with the TimeZone database

    • ClassLoader.getResourceAsStream() is used to load the TZ data and is not memory efficient • Joda-Time Android loads the TZ data from resources using AssetManager https://github.com/dlew/joda-time-android
  10. https://github.com/dlew/joda-time-android • Loading the TZ data at app startup can

    be time consuming • This can add 50-200ms to cold boot time for app Joda-Time Android - a caution
  11. https://github.com/dlew/joda-time-android • Loading the TZ data at app startup can

    be time consuming • This can add 50-200ms to cold boot time for apps • If possible, defer initialization till you need it Joda-Time Android - a caution
  12. https://blog.joda.org/2009/11/why-jsr-310-isn-joda-time_4941.html • Joda-Time has some design flaws • Pluggable Chronology

    • Complex internals • Nullability • Difference between Human and Machine times Why anything more?
  13. java.time • Introduced in Java 8 • But available only

    on API Level 26+ • Results in ClassNotFoundException before API Level 25
  14. ThreeTenBP • Backport of java.time library to be used on

    Java 6 and 7 • Ships TZ data along with the JAR
  15. ThreeTenBP • Backport of java.time library to be used on

    Java 6 and 7 • Ships TZ data along with the JAR • JakeWharton created ThreeTenABP
  16. ThreeTenBP • Backport of java.time library to be used on

    Java 6 and 7 • Ships TZ data along with the JAR • JakeWharton created ThreeTenABP • Can be used back to API Level 15
  17. Core Library Desugaring • Android Gradle Plugin 3.0.0+ added support

    for some Java 8 language features • AGP 4.0.0+ added support for desugaring Java 8 language API
  18. Core Library Desugaring • Android Gradle Plugin 3.0.0+ added support

    for some Java 8 language features • AGP 4.0.0+ added support for desugaring Java 8 language API • AGP 4.0.0+ supports a subset of java.time
  19. java.time without a min API android { defaultConfig { multiDexEnabled

    true } compileOptions { // 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' }
  20. ThreeTenABP -> java.time • Enable core library desugaring • Change

    all imports from org.threeten.bp to java.time • Profit
  21. Recap • Don't use java.util.Date • If on Joda-Time, no

    need to migrate • On API 21+ , use java.time using core library desugaring