a jsr-310 date: beyond joda time

54
BEYOND JODA TIME A JSR-310 DATE

Upload: daniel-sobral

Post on 10-May-2015

3.197 views

Category:

Technology


3 download

DESCRIPTION

A brief look at JSR-310, and why it exists at all.

TRANSCRIPT

Page 1: A JSR-310 Date: Beyond JODA Time

B E YO N D J O DA T I M E

A JSR-310 DATE

Page 2: A JSR-310 Date: Beyond JODA Time

IN THIS PRESENTATION

• What is JSR-310?• What’s wrong with Date, Calendar and

DateFormat?• Why not JODA Time?• JSR-310 Overview

Page 3: A JSR-310 Date: Beyond JODA Time

•What does it mean?•What does it have?WHAT IS

JSR-310?

Page 4: A JSR-310 Date: Beyond JODA Time

WHAT DOES JSR-310 MEANS?

• A Java Specification Request included on Java 8

Page 5: A JSR-310 Date: Beyond JODA Time

WHAT DOES JSR-310 MEANS?

• A Java Specification Request included on Java 8• A library to handle dates and times

Page 6: A JSR-310 Date: Beyond JODA Time

WHAT DOES JSR-310 MEANS?

• A Java Specification Request included 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.*

Page 7: A JSR-310 Date: Beyond JODA Time

WHAT DOES JSR-310 HAVE?

• Instants (timestamps)• Date and Time• Partial Date and Time• Parser and Formatter• Time zones• Different chronologies (calendars)

Page 8: A JSR-310 Date: Beyond JODA Time

• Bad Naming•Historical Issues•Design Problems•DeficienciesWHAT’S

WRONG WITH THE EXISTING CLASSES?

Page 9: A JSR-310 Date: Beyond JODA Time

BAD NAMING

• Horrible naming decisions

Page 10: A JSR-310 Date: Beyond JODA Time

BAD NAMING

• Horrible naming decisions• Date is not a date, it is an instant in time (a timestamp)

Page 11: A JSR-310 Date: Beyond JODA Time

BAD NAMING

• Horrible naming decisions• Date is not a date, it is an instant in time (a timestamp)• Date is not a time• The time field manipulation methods are deprecated

Page 12: A JSR-310 Date: Beyond JODA Time

BAD NAMING

• Horrible naming decisions• Date is not 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

Page 13: A JSR-310 Date: Beyond JODA Time

HISTORICAL ISSUES

• Date has no support for I18N or L10N

Page 14: A JSR-310 Date: Beyond JODA Time

HISTORICAL ISSUES

• Date has no support for I18N or L10N• So Sun added IBM-donated (through Taligent)

code• Calendar• TimeZone• SimpleDateFormat

Page 15: A JSR-310 Date: Beyond JODA Time

HISTORICAL ISSUES

• Date has no support for I18N or 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

Page 16: A JSR-310 Date: Beyond JODA Time

HISTORICAL ISSUES

• Date has no support for I18N or 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)

Page 17: A JSR-310 Date: Beyond JODA Time

DESIGN PROBLEMS

• They are mutable!

Page 18: A JSR-310 Date: Beyond JODA Time

DESIGN PROBLEMS

• They are mutable!• On core, excluding 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

Page 19: A JSR-310 Date: Beyond JODA Time

DESIGN PROBLEMS

• They are mutable!• On core, excluding 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

Page 20: A JSR-310 Date: Beyond JODA Time

DEFICIENCIES

• They are limited• My alarm clock rings at 6:30 AM

Page 21: A JSR-310 Date: Beyond JODA Time

DEFICIENCIES

• They are limited• My alarm clock rings at 6:30 AM (time without date or tz)• I was born March 23, 1971

Page 22: A JSR-310 Date: Beyond JODA Time

DEFICIENCIES

• They are limited• My alarm clock rings at 6:30 AM (time without date or tz)• I was born March 23, 1971(date without time)• My birthday is March 23

Page 23: A JSR-310 Date: Beyond JODA Time

DEFICIENCIES

• They are limited• My alarm clock rings 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

Page 24: A JSR-310 Date: Beyond JODA Time

DEFICIENCIES

• They are limited• My alarm clock rings 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

Page 25: A JSR-310 Date: Beyond JODA Time

DEFICIENCIES

• They are limited• My alarm clock rings 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)

Page 26: A JSR-310 Date: Beyond JODA Time

•Why JODA Time?• Too Flexible• Bad Internal Representation•Null-happy• JODA Time & JSR-310

WHY NOT JODA TIME?

Page 27: A JSR-310 Date: Beyond JODA Time

WHY JODA TIME?

• JODA Time addresses all these issues:• It is composed of immutable classes• It handles Instants, Date&Time, Partials, and Durations• It is flexible• It is well designed

Page 28: A JSR-310 Date: Beyond JODA Time

TOO FLEXIBLE

• Every class is prepared to handle the 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

Page 29: A JSR-310 Date: Beyond JODA Time

BAD INTERNAL REPRESENTATION

• Represents dates as instants• But 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

Page 30: A JSR-310 Date: Beyond JODA Time

NULL-HAPPY

• Accepts nulls as valid values on most of its methods• Leads to subtle bugs

Page 31: A JSR-310 Date: Beyond JODA Time

JODA TIME AND JSR-310

• JODA Time is Not Broken• But the lessons learned led to a new time library

design• JSR-310 is inspired by JODA Time, but simpler and

more robust

Page 32: A JSR-310 Date: Beyond JODA Time

• Basic classes•Now• Clocks and Testing• Parsing and Printing• Fields and Units• Java 7 Backport

JSR-310 OVERVIEW

Page 33: A JSR-310 Date: Beyond JODA Time

BASIC INTERFACES

Interface

• TemporalAccessor• Temporal• TemporalField• TemporalAmount• TemporalUnit• TemporalQuery• TemporalAdjuster

Purpose

• Readable date&time• Modifiable date&time• Date&time component• Amount of time• Unit of amount of time• Queries date&time• Modifies date&time

Page 34: A JSR-310 Date: Beyond JODA Time

TEMPORALACCESSOR

Page 35: A JSR-310 Date: Beyond JODA Time

TEMPORAL

Page 36: A JSR-310 Date: Beyond JODA Time

TEMPORALFIELD

Page 37: A JSR-310 Date: Beyond JODA Time

TEMPORALAMOUNT

Page 38: A JSR-310 Date: Beyond JODA Time

TEMPORALUNIT

Page 39: A JSR-310 Date: Beyond JODA Time

TEMPORALQUERY

Page 40: A JSR-310 Date: Beyond JODA Time

TEMPORALADJUSTER

Page 41: A JSR-310 Date: Beyond JODA Time

BASIC CLASSES

Class

• Instant• Clock• LocalDateTime• 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

Page 42: A JSR-310 Date: Beyond JODA Time

INSTANT

• A point in time• Time since 1970-01-01 00:00:00 UTC

• Useful for timestamps• Nanosecond resolution• Temporal, TemporalAccessor, TemporalAdjuster• Before/After• To/From seconds, milliseconds• Creates OffsetDateTime & ZonedDateTime

Page 43: A JSR-310 Date: Beyond JODA Time

LOCALDATETIME

• A time in years, months, days, hours, minutes 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

Page 44: A JSR-310 Date: Beyond JODA Time

ZONEDDATETIME/OFFSETDATETIME

• A date and time associated with either a 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

Page 45: A JSR-310 Date: Beyond JODA Time

DURATION & PERIOD

• Represent amounts of time• Duration 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

Page 46: A JSR-310 Date: Beyond JODA Time

PARTIAL DATES AND TIMES

• LocalDate• LocalTime• DayOfWeek• Month• MonthDay• YearMonth• Year

Page 47: A JSR-310 Date: Beyond JODA Time

WHAT TIME IS IT NOW?

• Instant.now()• default timezone

• 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

Page 48: A JSR-310 Date: Beyond JODA Time

CLOCK AND TESTING

• Clocks can be injected• Clocks 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

Page 49: A JSR-310 Date: Beyond JODA Time

PRINTING & PARSING

• DateTimeFormatter replaces SimpleDateFormat• Immutable, so 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);

Page 50: A JSR-310 Date: Beyond JODA Time

JSR-310 BACKPORT TO JAVA 7

• Fork of the original 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

Page 51: A JSR-310 Date: Beyond JODA Time

BENEFITS OF ADOPTING THE BACKPORT

• Much superior to existing Java classes• Closer to Java 8 than JODA, and less prone to

bugs• Easy transition to Java 8• Superior testability

Page 52: A JSR-310 Date: Beyond JODA Time

TRIVIAL EXAMPLE

Page 53: A JSR-310 Date: Beyond JODA Time

COMPLEX EXAMPLE 1

Page 54: A JSR-310 Date: Beyond JODA Time

COMPLEX EXAMPLE 2