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 Slide

  2. java.util.Date

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 Slide

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

    View Slide

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

    View Slide

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

    View 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

  19. Calendar

    View Slide

  20. Calendar
    • Cannot format Calendar instance directly

    View Slide

  21. Calendar
    • Cannot format Calendar instance directly
    • Mutable

    View Slide

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

    View Slide

  23. Joda-Time

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  28. 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 Slide

  29. 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 Slide

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

    View Slide

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

    View Slide

  32. 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 Slide

  33. 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 Slide

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

    View Slide

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

    View Slide

  36. 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 Slide

  37. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

  42. 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 Slide

  43. 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 Slide

  44. java.time

    View Slide

  45. java.time
    • Introduced in Java 8

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  50. 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 Slide

  51. 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 Slide

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

    View Slide

  53. Desugaring

    View Slide

  54. Core Library Desugaring

    View Slide

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

    View Slide

  56. 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 Slide

  57. 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 Slide

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

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

    View Slide

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

    View Slide

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

    View Slide

  62. 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 Slide

  63. @iamsubhrajyoti

    View Slide