Save 37% off PRO during our Black Friday Sale! »

Journey Of Time

Journey Of Time

53b72671be580e70c9795c7eaf35ac12?s=128

Subhrajyoti Sen

January 19, 2021
Tweet

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
  2. java.util.Date

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

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

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

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

    instance in Time Date = Date + Time
  7. java.util.Date val date = Date(2021, 1, 19)

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

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

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

    19, 2021? - No Year is 1900-based offset
  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
  12. java.util.Date val date = Date(2021, 1, 19) It is February

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

  14. java.util.Date val date = Date(2020, 0, 35) print(date.toString()) Expected -

    Exception. What is even January 35?
  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
  16. java.util.Date val date = Date(2021, 1, 19) val day =

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

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

    date.day Expected: 19 Actual: 3
  19. Calendar

  20. Calendar • Cannot format Calendar instance directly

  21. Calendar • Cannot format Calendar instance directly • Mutable

  22. Calendar • Cannot format Calendar instance directly • Mutable •

    Still no arithmetic functions
  23. Joda-Time

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

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

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

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

    for each concept • Immutable • Handy classes for arithmetic operations
  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
  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
  30. Joda-Time Android https://github.com/dlew/joda-time-android

  31. Joda-Time Android • Joda-Time JAR ships with the TimeZone database

    https://github.com/dlew/joda-time-android
  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
  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
  34. Joda-Time Android - a caution https://github.com/dlew/joda-time-android

  35. https://github.com/dlew/joda-time-android • Loading the TZ data at app startup can

    be time consuming Joda-Time Android - a caution
  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
  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
  38. https://blog.joda.org/2009/11/why-jsr-310-isn-joda-time_4941.html Why anything more?

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

  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?
  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?
  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?
  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?
  44. java.time

  45. java.time • Introduced in Java 8

  46. java.time • Introduced in Java 8 • But available only

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

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

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

    Java 6 and 7 • Ships TZ data along with the JAR
  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
  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
  52. Syntactic Sugar val array = arrayOf(3, 5, 6) array[1] val

    list = listOf(3, 5,6 ) list[1]
  53. Desugaring

  54. Core Library Desugaring

  55. Core Library Desugaring • Android Gradle Plugin 3.0.0+ added support

    for some Java 8 language features
  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
  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
  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' }
  59. ThreeTenABP -> java.time • Enable core library desugaring • Change

    all imports from org.threeten.bp to java.time • Profit
  60. Recap • Don't use java.util.Date

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

    need to migrate
  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
  63. @iamsubhrajyoti