on Java 8 • A library to handle dates and times • A set of packages: • java.time.* • java.time.chrono.* • java.time.format.* • java.time.temporal.* • java.time.zone.*
a date, it is an instant in time (a timestamp) • Date is not a time • The time field manipulation methods are deprecated • Calendar is not a calendar, it is a date and time
L10N • So Sun added IBM-donated (through Taligent) code • Calendar • TimeZone • SimpleDateFormat • Which don’t even play well together • SimpleDateFormat can’t be used to convert from or to Calendar
L10N • So Sun added IBM-donated (through Taligent) code • Calendar • TimeZone • SimpleDateFormat • Which don’t even play well together • SimpleDateFormat can’t be used to convert from or to Calendar • And still kept months 0-based • (but at least made years 0-based instead of 1900-based)
tests, SimpleDateTime is: • instantiated in 225 places • a field in 77 places • usually synchronized (if correct) • a local variable in 103 places • Which could be replaced with a dozen immutable static constants
tests, SimpleDateTime is: • instantiated in 225 places • a field in 77 places • usually synchronized (if correct) • a local variable in 103 places • Which could be replaced with a dozen immutable static constants • Calendar stores redundant representations, and recomputes lazily depending on the method being called
at 6:30 AM (time without date or tz) • I was born March 23, 1971(date without time) • My birthday is March 23 (date without year) • This presentation is one hour long
at 6:30 AM (time without date or tz) • I was born March 23, 1971(date without time) • My birthday is March 23 (date without year) • This presentation is one hour long (a duration) • A year has twelve months
at 6:30 AM (time without date or tz) • I was born March 23, 1971(date without time) • My birthday is March 23 (date without year) • This presentation is one hour long (a duration) • A year has twelve months (a period)
most obscure calendar systems (pluggable chronology) • But code usually assumes Gregorian Calendar • (have you ever written code that handles 13 months?) • int month = dateTime.getMonthOfDay(); • It lacks type safety
a date&time may correspond to more than one instant • Overlap hour when daylight savings end • As well as not have any instant that corresponds to it at all • Gap hour when daylight starts • Has to perform complex computations for simple operations
OffsetDateTime • ZonedDateTime • Duration • Period Purpose • Moment in time • Instant Factory • Arbitrary Date and Time • Date & Time with UTC offset • Date & Time with TZ • Difference between instants • Duration in time units
and seconds • No timezone • Does not have an instant associated with it • ISO-8601 Calendar System, not Gregorian • Temporal, TemporalAccessor, TemporalAdjuster • Before/After • Manipulate with years, months, weeks, days, hours, minutes, seconds and nanos • Creates OffsetDateTime & ZonedDateTime
particular time zone, or a particular time zone offset • OffsetDateTime always have an associated instant • ZonedDateTime have gaps and overlaps • One can get a ZDT that returns either the earlier or the later time in an overlap • Temporal, TemporalAccessor • Before/After
is an amount of nanoseconds • Period is an amount of years, months and days • Can be computed in absolute terms • Duration.ofMinutes(10) • Can be computed from dates and instants • Period.between(LocalDate.now(), birthDay) • Can be added of subtracted from instants, dates and times
• Instant.now(ZoneId zone) • time at a specific time zone • Instant.now(Clock clock) • time as generated by a specific clock • LocalTime.now() • MonthDay.now(clock) • ZonedDateTime(zone) • etc
can be created with various properties • Static clocks • Mocked clocks • Low-precision clocks (whole seconds, whole minutes, etc) • Clocks can be created with specific time zones • Clock.system(Zone.of(“America/Los_Angeles”)) • Makes code handling date and time testable • Makes tests independent of timezone
reusable and thread-safe • Many pre-defined styles • DateTimeFormatterBuilder • All instant, date and time classes use it the same way: • Instant t = Instant.parse(input, dateTimeFormatter); • String ts = ZonedDateTime.format(dateTimeFormatter); • Duration, Period and other classes have fixed formats: • Duration d = Duration.parse(repr);
implementation of JSR-310, under the BSD license, before it got added to JDK 8 • Presently equivalent to milestone 7 of JDK 1.8 • When JDK 1.8 comes out, a new release of the backport will be made • Presently diverging from JDK 1.8 • org.threeten, threetenbp, 8.1 • package org.threeten.bp instead of java.time