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. Journey Of Time
    KeepTruckin
    Subhrajyoti Sen Android Worldwide 2021
    N O T A P H Y S I C S T A L K

    View full-size slide

  2. java.util.Date

    View full-size slide

  3. java.util.Date
    Date is not a date

    View full-size slide

  4. java.util.Date
    Date is not a date

    View full-size slide

  5. java.util.Date
    Date is not a date
    A Date in an instance in Time

    View full-size slide

  6. java.util.Date
    Date is not a date
    A Date in an instance in Time
    Date = Date + Time

    View full-size slide

  7. java.util.Date
    val date = Date(2021, 1, 19)

    View full-size slide

  8. java.util.Date
    val date = Date(2021, 1, 19)
    Is this January 19, 2021?

    View full-size slide

  9. java.util.Date
    val date = Date(2021, 1, 19)
    Is this January 19, 2021? - No

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  12. java.util.Date
    val date = Date(2021, 1, 19)
    It is February 19, 3921

    View full-size slide

  13. java.util.Date
    val date = Date(2020, 0, 35)
    print(date.toString())

    View full-size slide

  14. java.util.Date
    val date = Date(2020, 0, 35)
    print(date.toString())
    Expected - Exception. What is even January 35?

    View full-size slide

  15. 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

    View full-size slide

  16. java.util.Date
    val date = Date(2021, 1, 19)
    val day = date.day

    View full-size slide

  17. java.util.Date
    val date = Date(2021, 1, 19)
    val day = date.day
    Expected: 19

    View full-size slide

  18. java.util.Date
    val date = Date(2021, 1, 19)
    val day = date.day
    Expected: 19
    Actual: 3

    View full-size slide

  19. Calendar
    • Cannot format Calendar instance directly

    View full-size slide

  20. Calendar
    • Cannot format Calendar instance directly
    • Mutable

    View full-size slide

  21. Calendar
    • Cannot format Calendar instance directly
    • Mutable
    • Still no arithmetic functions

    View full-size slide

  22. Joda-Time
    • De-facto solution before Java 8

    View full-size slide

  23. Joda-Time
    • De-facto solution before Java 8
    • Separate classes for each concept

    View full-size slide

  24. Joda-Time
    • De-facto solution before Java 8
    • Separate classes for each concept
    • Immutable

    View full-size slide

  25. Joda-Time
    • De-facto solution before Java 8
    • Separate classes for each concept
    • Immutable
    • Handy classes for arithmetic operations

    View full-size slide

  26. 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

    View full-size slide

  27. 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

    View full-size slide

  28. Joda-Time Android
    https://github.com/dlew/joda-time-android

    View full-size slide

  29. Joda-Time Android
    • Joda-Time JAR ships with the TimeZone database
    https://github.com/dlew/joda-time-android

    View full-size slide

  30. 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

    View full-size slide

  31. 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

    View full-size slide

  32. Joda-Time Android - a caution
    https://github.com/dlew/joda-time-android

    View full-size slide

  33. https://github.com/dlew/joda-time-android
    • Loading the TZ data at app startup can be time consuming
    Joda-Time Android - a caution

    View full-size slide

  34. 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

    View full-size slide

  35. 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

    View full-size slide

  36. https://blog.joda.org/2009/11/why-jsr-310-isn-joda-time_4941.html
    Why anything more?

    View full-size slide

  37. https://blog.joda.org/2009/11/why-jsr-310-isn-joda-time_4941.html
    • Joda-Time has some design flaws
    Why anything more?

    View full-size slide

  38. https://blog.joda.org/2009/11/why-jsr-310-isn-joda-time_4941.html
    • Joda-Time has some design flaws
    • Pluggable Chronology
    Why anything more?

    View full-size slide

  39. https://blog.joda.org/2009/11/why-jsr-310-isn-joda-time_4941.html
    • Joda-Time has some design flaws
    • Pluggable Chronology
    • Complex internals
    Why anything more?

    View full-size slide

  40. 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
    Why anything more?

    View full-size slide

  41. 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?

    View full-size slide

  42. java.time
    • Introduced in Java 8

    View full-size slide

  43. java.time
    • Introduced in Java 8
    • But available only on API Level 26+

    View full-size slide

  44. java.time
    • Introduced in Java 8
    • But available only on API Level 26+
    • Results in ClassNotFoundException before API Level 25

    View full-size slide

  45. ThreeTenBP
    • Backport of java.time library to be used on Java 6 and 7

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  48. 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

    View full-size slide

  49. Syntactic Sugar
    val array = arrayOf(3, 5, 6)
    array[1]
    val list = listOf(3, 5,6 )
    list[1]

    View full-size slide

  50. Core Library Desugaring

    View full-size slide

  51. Core Library Desugaring
    • Android Gradle Plugin 3.0.0+ added support for some Java 8 language
    features

    View full-size slide

  52. 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

    View full-size slide

  53. 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

    View full-size slide

  54. 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'
    }

    View full-size slide

  55. ThreeTenABP -> java.time
    • Enable core library desugaring
    • Change all imports from org.threeten.bp to java.time
    • Profit

    View full-size slide

  56. Recap
    • Don't use java.util.Date

    View full-size slide

  57. Recap
    • Don't use java.util.Date
    • If on Joda-Time, no need to migrate

    View full-size slide

  58. 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

    View full-size slide

  59. @iamsubhrajyoti

    View full-size slide