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

I have a Date

I have a Date

One common task in Android app development is working with Date objects, and Kotlin provides several options for managing it.
In this talk, we will explore the different ways to represent and manipulate dates in Kotlin, including using the built-in java.util.Date class, the third-party Joda-Time and ThreeTen libraries, and the java.time package introduced in Java 8.

We will also discuss common problems, and handling time zones. By the end of this talk, attendees will have a strong understanding of how to effectively manage dates in their Kotlin Android apps.

Renaud MATHIEU

April 27, 2023
Tweet

More Decks by Renaud MATHIEU

Other Decks in Programming

Transcript

  1. Current year according to various historical and world calendars, as

    of January 1. 2023 0 1500 3000 4500 6000 Hebrew Chinese Gregorian Islamic French Revolutionary 230 1   2   4   5  
  2. UTC Français TUC Temps universel coordonné Anglais CUT Coordinated Universal

    Time 🤝 UTC Universal Time Coordinated Universel Temps Coordonné Coordinated Universal Time
  3. 🛠 Unix Epoch • Unix time is a date and

    time representation widely used in computing. • Leap seconds* are known only 6 months in advance. • Year 2038 problem *leap seconds = secondes intercalaires 🇫🇷 MACHINE TIME MANAGEMENT
  4. 2023-04-27T15:50:00+02:00 YYYY-MM-DD or YYYYMMDD YYYY-MM (but not YYYYMM) Thh:mm:ss.sss or

    Thhmmss.sss Thh:mm:ss or Thhmmss Thh:mm or Thhmm Thh <time>Z <time>±hh:mm <time>±hhmm <time>±hh -YY-MM or -YYMM —MM-DD or —MMDD - - MM - - - DD
  5. History of Time, in a Java world… 1996 Java 1.0

    java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime
  6. History of Time, in a Java world… 1996 Java 1.0

    java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime
  7. java.util.Date import java.util.Date fun main() { val date = Date().apply

    { year = 2023 month = 4 day = 27 } print(date) } / / Fri May 04 21:18:05 UTC 3923
  8. History of Time, in a Java world… 1996 Java 1.0

    java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime
  9. History of Time, in a Java world… 1996 Java 1.0

    java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime
  10. Joda Time A Powerful and Versatile Date and Time Library

    • Provide an alternative to the JDK date management classes which have several drawbacks. • Aimed to provide a simpler and more intuitive API for handling dates and times.
  11. History of Time, in a Java world… 1996 Java 1.0

    java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime
  12. History of Time, in a Java world… 1996 Java 1.0

    java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime
  13. Machine Time Instant • Encapsulates a point in time with

    nanosecond precision. • Represent a point in the timeline. • The Instant class is the closest class to the java.util.Date class I MACHINE TIME MANAGEMENT
  14. Machine Time Instant import java.time.Instant fun main() { println(Instant.EPOCH) println(Instant.now())

    } —— 1970-01-01T00:00:00Z 2023-04-26T09:21:39.149061Z MACHINE TIME MANAGEMENT
  15. Machine Time Duration • Encapsulates a time between two instants

    stored with nanosecond precision. • Duration can be negative! • The ISO-8601 standard de fi nes the textual format of a duration: PTnHnMn.nS MACHINE TIME MANAGEMENT
  16. import java.time.Instant import java.time.Duration import java.time.temporal.ChronoUnit fun main() { var

    oneHour = Duration.ofHours(1L) println("oneHour = $oneHour") oneHour = Duration.ofMinutes(60L) println("oneHour = $oneHour") oneHour = Duration.ofSeconds(3600L) println("oneHour = $oneHour") val instant1 = Instant.parse("2023-04-26T23:59:59Z") val instant2 = instant1.plus(1L, ChronoUnit.DAYS) val duration = Duration.between(instant1, instant2) println("duration = $duration") } oneHour = PT1H oneHour = PT1H oneHour = PT1H duration = PT24H
  17. LocalDate LocalTime LocalDateTime Period Duration LocalDate private final int year;

    private final short month; private final short day; val date1 = LocalDate.now() val date2 = LocalDate.of(1987, Month.JULY, 6) val date3 = LocalDate.ofEpochDay(113) println(date1) println(date2) println(date3) —— 2023-07-05 1987-07-06 1970-04-24
  18. LocalDate LocalTime LocalDateTime Period Duration val time1 = LocalTime.now() val

    time2 = LocalTime.of(23,59,59,104534) println(time1) println(time2) println(time1.minusHours(1)) —— 10:06:27.256581 23:59:59.000104534 09:06:27.256581 LocalTime private final byte hour; private final byte minute; private final byte second; private final int nano;
  19. LocalDate LocalTime LocalDateTime Period Duration val date = LocalDate.now() val

    time = LocalTime.now() val dateTime1: LocalDateTime = date.atTime(time) val dateTime2 = time.atDate(date) println(dateTime1) println(dateTime2) println(dateTime1.dayOfWeek) —— 2023-07-04T15:42:32.878386241 2023-07-04T15:42:32.878386241 TUESDAY LocalDateTime private final LocalDate date; private final LocalTime time; LocalTime LocalDate
  20. LocalDate LocalTime LocalDateTime Period Duration val date = LocalDate.now().minusWeeks(2) val

    dateTime = LocalDateTime.now() val period = Period.between(date,dateTime.toLocalDate()) println(date) println(dateTime) println(period) —— 2023-06-20 2023-07-04T15:43:57.093814609 P14D Period private final int years; private final int months; private final int days;
  21. LocalDate LocalTime LocalDateTime Period Duration val time1 = LocalTime.now() val

    time2 = LocalTime.now().minusMinutes(40) val duration = Duration.between(time1, time2) println(duration) —— PT-39M-59.999147S Duration private final long seconds; private final int nanos;
  22. Time Zones ZoneId • Encapsulates a time zone identi fi

    ed by its unique id: « Europe/Paris » • Contains rules to transform Instant to LocalDateTime
  23. Time Zones ZoneId + ZoneRules • There are three types

    of IDs: • simple: corresponds to the one encapsulated in a ZoneOffset. Its representation begins with the letter Z then the sign + or - and the offset value • offset-style: starts with a pre fi x (UTC, GMT or UT) then the + or - sign and the offset value • region-based: starts with at least two characters different from the offset-style pre fi xes and the characters + and -,
  24. val zoneId: ZoneId = ZoneId.of("Europe/Paris") val rules = zoneId.getRules() println(rules.getOffset(LocalDateTime.of(2023,

    Month.MARCH,26, 0, 0))) println(rules.getOffset(LocalDateTime.of(2023, Month.MARCH, 27, 0, 0))) +01:00 +02:00
  25. ZonedDateTime vs OffsetDateTime • ZonedDateTime : • is based on

    the time zone identi fi er, such as « America/New_York » • is used when you need to represent date and time information in different time zones • OffsetDateTime: • is based on the offset from UTC, such as « +03:00 ». • is used when you need to represent date and time information with a known UTC offset.
  26. History of Time, in a Java world… 1996 Java 1.0

    java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime
  27. History of Time, in a Java world… 1996 Java 1.0

    java.util.date 1997 Java 1.1 java.util.calendar 2005 joda-time 2014 JSR-310 2015 ThreeTenABP 2020 kotlinx-datetime
  28. kotlinx-datetime Bene fi ts public fun DateTimePeriod( years: Int =

    0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0, nanoseconds: Long = 0 ): DateTimePeriod = buildDateTimePeriod(totalMonths(years, months), days, totalNanoseconds(hours, minutes, seconds, nanoseconds)) • It's in the alpha stage, and the development continues. • Multiplatform support • @Serializable • Simpler API
  29. kotlinx-datetime Bene fi ts • It's in the alpha stage,

    and the development continues. • Multiplatform support • @Serializable • Simpler API // Support ISO-8601 format "2010-06-01T22:19:44.475Z".toInstant() "2010-06-01T22:19:44".toLocalDateTime() "2010-06-01".toLocalDate() "12:01:03".toLocalTime() "12:0:03.999".toLocalTime()
  30. Project Mainline Modular System Components IANA releases an update to

    the 
 Time Zone Database Google or the Android partner prepares a Time Zone Data module update (APEX fi le) End-user device downloads the update, reboots, then applies the changes