72
Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos TS-6578

Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

Its high time!JSR-310 – A new date and time API

Stephen ColebourneMichael Santos

TS-6578

Page 2: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 2

Understand Java Specification Request (JSR) 310 - Date and Time API

Page 3: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 3

Agenda

Problems todayJSR-310 overviewContinuousHumanIntegrationPeriodsMiscellaneous

Page 4: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 4

Spot the bugs...

Date date = new Date(2007, 12, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/HongKong");Calendar cal = new GregorianCalendar(date, zone);

DateFormat fm = new SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);

Page 5: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 5

Spot the bugs...

Date date = new Date(2007, 12, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/HongKong");Calendar cal = new GregorianCalendar(date, zone);

DateFormat fm = new SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);

Page 6: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 6

Spot the bugs...

int year = 2007 - 1900;Date date = new Date(year, 12, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/HongKong");Calendar cal = new GregorianCalendar(date, zone);

DateFormat fm = new SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);

Page 7: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 7

Spot the bugs...

int year = 2007 - 1900;Date date = new Date(year, 12, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/HongKong");Calendar cal = new GregorianCalendar(date, zone);

DateFormat fm = new SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);

Page 8: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 8

Spot the bugs...

int year = 2007 - 1900;int month = 12 - 1;Date date = new Date(year, month, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/HongKong");Calendar cal = new GregorianCalendar(date, zone);

DateFormat fm = new SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);

Page 9: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 9

Spot the bugs...

int year = 2007 - 1900;int month = 12 - 1;Date date = new Date(year, month, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/HongKong");Calendar cal = new GregorianCalendar(date, zone);

DateFormat fm = new SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);

Page 10: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 10

Spot the bugs...

int year = 2007 - 1900;int month = 12 - 1;Date date = new Date(year, month, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/Hong_Kong");Calendar cal = new GregorianCalendar(date, zone);

DateFormat fm = new SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);

Page 11: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 11

Spot the bugs...

int year = 2007 - 1900;int month = 12 - 1;Date date = new Date(year, month, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/Hong_Kong");Calendar cal = new GregorianCalendar(date, zone);

DateFormat fm = new SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);

Page 12: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 12

Spot the bugs...

int year = 2007 - 1900;int month = 12 - 1;Date date = new Date(year, month, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/Hong_Kong");Calendar cal = new GregorianCalendar(zone);cal.setTime(date);DateFormat fm = new SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);

Page 13: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 13

Spot the bugs...

int year = 2007 - 1900;int month = 12 - 1;Date date = new Date(year, month, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/Hong_Kong");Calendar cal = new GregorianCalendar(zone);cal.setTime(date);DateFormat fm = new SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);

Page 14: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 14

Spot the bugs...

int year = 2007 - 1900;int month = 12 - 1;Date date = new Date(year, month, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/Hong_Kong");Calendar cal = new GregorianCalendar(zone);cal.setTime(date);DateFormat fm = new SimpleDateFormat("HH:mm Z");Date calDate = cal.getTime();String str = fm.format(calDate);

Page 15: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 15

Spot the bugs...

int year = 2007 - 1900;int month = 12 - 1;Date date = new Date(year, month, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/Hong_Kong");Calendar cal = new GregorianCalendar(zone);cal.setTime(date);DateFormat fm = new SimpleDateFormat("HH:mm Z");Date calDate = cal.getTime();String str = fm.format(calDate);

Page 16: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 16

Spot the bugs...

int year = 2007 - 1900;int month = 12 - 1;Date date = new Date(year, month, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/Hong_Kong");Calendar cal = new GregorianCalendar(zone);cal.setTime(date);DateFormat fm = new SimpleDateFormat("HH:mm Z");fm.setTimeZone(zone);Date calDate = cal.getTime();String str = fm.format(calDate);

Page 17: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 17

Spot the bugs...

Date date = new Date(2007, 12, 13, 16, 40);TimeZone zone = TimeZone.getInstance("Asia/HongKong");Calendar cal = new GregorianCalendar(date, zone);

DateFormat fm = new SimpleDateFormat("HH:mm Z");

String str = fm.format(cal);6 bugs !

Page 18: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 18

Existing API flaws

MutableJanuary is 0, December is 11Date is not a dateDate uses years from 1900Calendar cannot be formattedDateFormat not thread-safeSQL Date/Time/Timestamp extend Date

Page 19: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 19

High time for a new API

Time problems well known• Joda-Time - http://joda-time.sourceforge.net

Desire for improvements in Java Platform, Standard Edition 7 (Java SE platform 7)*JSR-310 started February 2007• JSR-310 - http://jsr-310.dev.java.net

* No commitments have been made about inclusion

Page 20: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 20

Agenda

Problems todayJSR-310 overviewContinuousHumanIntegrationPeriodsMiscellaneous

Page 21: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 21

JSR-310 Overview

Comprehensive model for date and timeType-safe• avoid primitives• self documenting• IDE friendly

Interoperate with existing classesConsider XML and Database

Page 22: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 22

Design principles

Guide designHelp decision makingDerived from other librariesDerived from experience

Page 23: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 23

Design principles – Immutable

No change after constructionThread-safeCan be singletons

Implementation considerations• classes and fields are final• construction typically by factory• 'with' methods instead of 'set'

• date.setDayOfMonth(12); X• date = date.withDayOfMonth(12);

Page 24: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 24

Design principles – Fluent

Easy to readEasy to learnLike a sentence

Implementation considerations• builder pattern• method names that 'flow'

Page 25: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 25

Design principles – Clear, Explicit and Expected

Each method is well-definedJavadoc easily explains what method doesNo coupling between methods

Implementation considerations• few super/subclasses• no optional/pluggable state• long/complex javadoc → refactor

Page 26: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 26

Design principles – Extensible

Many weird ways to manipulate timeJSR authors don't know everythingAllow for extensions, but avoid confusion

Implementation considerations• strategy pattern• default strategy for most use cases• clear javadoc for default

Page 27: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 27

Analysing time

'Time' has many meanings• time-dimension• time-line• time-point• time-interval• time-duration• time-position

How can we make sense of it?

Page 28: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 28

Continuous and Human

Two basic time-scales• way of 'counting' time

Continuous• single incrementing number• designed for machines

Human• field-based (year,month,day,hour,minute,second)• designed for humans

Page 29: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 29

Agenda

Problems todayJSR-310 overviewContinuousHumanIntegrationPeriodsMiscellaneous

Page 30: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 30

Instant

Single instantaneous point on the time-line• 53628746263276 nanoseconds after the epoch

Used to store a timestampNanosecond precision for age of universe• problem – no suitable primitive – 96 bits

Java class – Instant

Instant

Time-line

Page 31: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 31

Interval

Interval of time on the time-line• from start instant to end instant

Start inclusive, end exclusive• may support flexible inclusive/exclusive

Java class – InstantInterval• or maybe Interval<Instant>

Start Instant

Time-line

End InstantInterval

Page 32: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 32

Duration

Duration of time• 358753871581 nanoseconds

Fundamental scientific quantity• not connected to the time-line

Nanosecond precisionJava class – Duration

Time-lineDuration

Page 33: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 33

Maths

Instant + Duration = Instant

Duration + Duration = Duration

Duration = 'Length' of an Interval

Page 34: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 34

Examples

// instantInstant start = millisInstant(123450L);Instant end = instant(223L, 450000000);assert start.isBefore(end);assert end.isAfter(start);// intervalInstantInterval interval = interval(start,end);assert interval.contains(start);// durationDuration duration = durationOf(interval);Duration bigger = duration.multipliedBy(4);Duration biggest = bigger.plus(duration);Instant later = start.plus(duration);

Page 35: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 35

Agenda

Problems todayJSR-310 overviewContinuousHumanIntegrationPeriodsMiscellaneous

Page 36: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 36

Human-scale overview

Human-scale dates and times• field based• year, month, day, hour, minute, second

Requirements• Date and time• Date without time• Time without date• Time zone

Page 37: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 37

ISO-8601

Standard interchange formatBasis for other standards• XML schema

Includes• date• time• date-time• zone offset (from UTC)• period

No support for time zone rules

Page 38: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 38

ISO-8601

yyyy-MM-dd• 2007-12-03

hh:mm:ss.SSSZ• 11:05:30.123+01:00

yyyy-MM-dd'T'HH:mm:ss.SSSZ• 2007-12-03T11:05:30.123+01:00

Page 39: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 39

Analysis

Start from ISO-8601• yyyy-MM-dd'T'HH:mm:ss.SSSZ

Generalise• {date}T{time}{offset}

Date – year, month, dayTime – hour, minute, second, nanosecondOffset – from +14:00 to -12:00

Page 40: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 40

Classes

{date}T{time}{offset}One class for each part• LocalDate• LocalTime• ZoneOffset

One class for each combination• LocalDateTime: LocalDate + LocalTime• OffsetDate: LocalDate + ZoneOffset• OffsetTime: LocalTime + ZoneOffset• OffsetDateTime: LocalDateTime + ZoneOffset

Page 41: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 41

Calendar fields

{year}-{month}-{day}One class for each part• Year• MonthOfYear – Enum

• DayOfMonthExtend to other related concepts• DayOfWeek – Enum

• DayOfYear• and so on

Page 42: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 42

Calendar fields

{hour}:{minute}:{second}One class for each part• HourOfDay• MinuteOfHour• SecondOfMinute

Extend to other related concepts• NanoOfSecond• and so on

Page 43: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 43

Basic querying

Query date/time using 'get' methodsMethod returns object/enum• no primitives

LocalDate date = date(2007, 12, 3);Year year = date.getYear();int yearValue = year.getValue();boolean leap = year.isLeap();Era era = year.getEra();

Page 44: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 44

Matchers

Need to query parts of a date/timeQuery can be simple or complex• is the year 2006 ?• is the date the last day of the year ?

Strategy pattern – DateMatcherpublic interface DateMatcher { boolean matchesDate(LocalDate input);}boolean matches = date.matches(year(2006));boolean matches = date.matches(lastDayOfYear());

Page 45: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 45

Adjusters

Need to change a date/timeChange can be simple or complex• change the year to 2006• change the date to the last day of the month

Strategy pattern – DateAdjusterpublic interface DateAdjuster { LocalDate adjustDate(LocalDate input);}LocalDate adjusted = date.with(year(2006));LocalDate adjusted = date.with(lastDayOfMonth());

Page 46: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 46

Resolvers

Need to handle invalid date/time• February 30th

Strategy pattern – DateResolver

public interface DateResolver { LocalDate resolveDate( Year year, MonthOfYear month, DayOfMonth day);}DateResolver res = DateResolvers.previousValid();LocalDate date = date(2007, 2, 30, res);// date = 2007-02-28

Page 47: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 47

Time zones

Rules for how zone offset changes• Daylight Savings• 'Permanent' offset changes

Rules change frequently• Syria changed DST with 3 days notice• Western Australia has 3 year DST experiment• Brazil changes DST every year

Rules based on Olson database• Represented by id – 'Europe/London'

Complex

Page 48: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 48

Time zones

JDK™ software javadoc:• “TimeZone represents a time zone offset, and also figures out daylight savings”

JSR-310 separates responsibility• ZoneOffset – from +14:00 to -12:00• TimeZone – rules for switching zone offsets

Only one additional date-time class• ZonedDateTime• (ZonedDate and ZonedTime are nonsense)

Page 49: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 49

Zone resolvers

Need to handle invalid time due to time zones• Spring Daylight Savings 'gap'• Autumn/Fall Daylight Savings 'overlap'

Strategy pattern – ZoneResolver

// one day before DST ends (overlap of one hour)TimeZone zone = timeZone("Europe/London");ZonedDateTime dt = dateTime(2007,10,27,1,30,zone);// dt = 2007-10-27 01:30 +01:00ZoneResolver res = ZoneResolvers.retainOffset();dt = dt.plus(days(1), res);// dt = 2007-10-28 01:30 +01:00

Page 50: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 50

Date/time class summary

LocalDate 2007-12-03LocalTime 11:05:30LocalDateTime 2007-12-03T11:05:30OffsetDate 2007-12-03 +01:00OffsetTime 11:05:30+01:00OffsetDateTime 2007-12-03T11:05:30+01:00ZonedDateTime 2007-12-03T11:05:30+01:00

Europe/Paris

Page 51: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 51

Agenda

Problems todayJSR-310 overviewContinuousHumanIntegrationPeriodsMiscellaneous

Page 52: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 52

Integration via interfaces

Simple interfaces link everything• ReadableDate• ReadableTime• ReadableDateTime• ReadableInstant

Each provides one method• toLocalDate()• toLocalTime()• toLocalDateTime()• toInstant()

Page 53: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 53

Date/time integration

ZonedDateTime OffsetDateTime LocalDateTime

LocalTime

ReadableDateTime

ReadableDate

OffsetDate OffsetTimeLocalDate

ReadableTime

Page 54: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 54

Date/time integration

ZonedDateTime OffsetDateTime LocalDateTime

LocalTime

ReadableDateTime

ReadableDate

OffsetDate OffsetTimeLocalDate

ReadableTime

java.util.GregorianCalendar

java.sql.Date java.sql.Time

Page 55: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 55

Instant integration

ZonedDateTime OffsetDateTime

ReadableInstant

Instant

Page 56: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 56

Instant integration

ZonedDateTime OffsetDateTime

ReadableInstant

Instant java.util.Date

Page 57: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 57

Integration – Exising JDK classes

All old JDK software date/time classes will:• Implement JSR-310 interfaces• Be constructable from JSR-310 interfaces• Not be deprecated

All JSR-310 classes will:• Not reference the old JDK software date/time classes

Page 58: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 58

Integration – Databases

JDBC group represented on JSR-310Classes map onto SQL• LocalDate DATE• LocalTime TIME WITHOUT TIME ZONE• LocalDateTime TIMESTAMP WITHOUT TIMEZONE• OffsetTime TIME WITH TIME ZONE• OffsetDateTime TIMESTAMP WITH TIME ZONE

Open issue on time zone mapping• DB time zone id differs from Java

Page 59: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 59

Integration – XML

XML opinions represented on JSR-310Classes map onto XML• ReadableDate xs:date• ReadableTime xs:time• ReadableDateTime xs:datetime• YearMonth xs:gYearMonth• MonthDay xs:gMonthDay• Year xs:gYear• MonthOfYear xs:gMonth• DayOfMonth xs:gDay

Page 60: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 60

Agenda

Problems todayJSR-310 overviewContinuousHumanIntegrationPeriodsMiscellaneous

Page 61: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 61

Periods

Describe duration in human fields• 6 years, 2 months and 12 days

Use cases• meeting length – 2 hours• conference length – 5 days• summer holiday – 6 weeks

Page 62: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 62

Periods – Analysis

Example: 6 years, 2 months and 12 daysGeneralise• {years}{months}{days}

One class for each part• Years• Months• Days• and so on

One class for combination• Period

Page 63: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 63

Agenda

Problems todayJSR-310 overviewContinuousHumanIntegrationPeriodsMiscellaneous

Page 64: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 64

Formatting and Parsing

ISO-8601 returned by toString()Formatting• work in progress

Parsing• work in progress

Probably based on Joda-Time

Page 65: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 65

Calendar systems

Everything based on ISO-8601• current 'civil' calendar• not historically accurate

Requirements• support common calendars in JDK software• not overcomplicate main use case• no ambiguity in API

Page 66: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 66

Calendar systems

Simple classes for other calendars• HebrewDate• HinduDate• IslamicDate• JapaneseDate• ThaiDate• and so on

Subclass ObjectImplement ReadableDateConstruct from ReadableDate

Page 67: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 67

Current time

Affected by time zone• often forgotten

Requirements• stop time for test case• change to time in future/past• run time slowly

Page 68: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 68

Current time

Access current time using an object• avoid singleton• allows Inversion of Control

Anyone can implement a subclass• you can control time

// system millis, default time zoneInstant instant = Clock.system().instant();LocalDate date = Clock.system().today();// system millis, specified time zoneTimeZone zone = timeZone("Europe/Moscow");LocalDate date = Clock.system(zone).today();

Page 69: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 69

Current time

Supports Inversion of Control• inject Clock• could be a 'stop time' subclass for testing

public class MyForm { @Resource private Clock clock; // injected public void validate(LocalDate date) { if (date.isBefore(clock.today())) { // error } }}

Page 70: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 70

Summary

Current JDK date/time has problems• Joda-Time is the best current alternative

JSR-310 underway• Continuous

• timestamps• durations

• Human• dates and times• periods

• Formatting/parsing• Multiple calendar systems• Control over current time

Page 71: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 71

Summary

JSR-310 is open...• ...very open!

Help make sure this date/time API works!• join the mailing list• comment on the wiki• review the API – javadoc or svn

http://jsr-310.dev.java.net

Page 72: Its high time! JSR-310 – A new date and time API › technetwork › server-storage › ts... · Its high time! JSR-310 – A new date and time API Stephen Colebourne Michael Santos

2008 JavaOneSM Conference | java.sun.com/javaone | 72

Stephen ColebourneMichael Santos

TS-6578