27
Java Date and Time Past and Future Iulian Dogariu Ness Technologies Iași Java User Group 1379518200000

Java Date and Time - Past and Future

Embed Size (px)

DESCRIPTION

Reasoning correctly about dates and times seems easy but is not. Designing a good date/time API is even harder. We will be looking at how Java got it wrong so far, and how things will change with the upcoming JDK 8. Presentation for the Iasi JUG, 18 September 2013

Citation preview

Page 1: Java Date and Time - Past and Future

JavaDate and Time

Past and Future

Iulian DogariuNess Technologies

Iași Java User Group1379518200000

Page 2: Java Date and Time - Past and Future

APIs that make me happy

Page 3: Java Date and Time - Past and Future
Page 4: Java Date and Time - Past and Future
Page 5: Java Date and Time - Past and Future

Timestamp

Page 6: Java Date and Time - Past and Future

“System.currentTimeMillis() should be enough for anybody”

-- Bill Gates,after seeing the JDK 1.0 Javadocs

(well, not really)

Page 7: Java Date and Time - Past and Future

java.util.Date

Page 8: Java Date and Time - Past and Future

java.util.Calendar

Page 9: Java Date and Time - Past and Future

POLA !

Page 10: Java Date and Time - Past and Future
Page 11: Java Date and Time - Past and Future

java.util.Calendar, as of JDK 7

• Missing abstractions• Full of bad surprises– Unsound design – violates Liskov– Violates POLA– Getter methods with side effects!– Not thread safe– Untestable

Page 12: Java Date and Time - Past and Future
Page 13: Java Date and Time - Past and Future
Page 14: Java Date and Time - Past and Future

Instants

Page 15: Java Date and Time - Past and Future

Instants

1379518200000

1379518274000

2013-09-18T15:30Z

2013-09-18T15:31:14Z

2013-09-18T18:30+03:00

2013-09-18T18:31:14+03:00

Page 16: Java Date and Time - Past and Future

Civil time

Page 17: Java Date and Time - Past and Future

Instants ↔ Civil time

Page 18: Java Date and Time - Past and Future

Let’s Practice !

Page 19: Java Date and Time - Past and Future

Java 8 Time vs Joda Time

Page 20: Java Date and Time - Past and Future

We love .NET too

http://nodatime.org/

Page 21: Java Date and Time - Past and Future
Page 22: Java Date and Time - Past and Future

Java 8 Time – Recap

• Instants• Civil time (Days, Months, Weeks)• Zones and Offsets

• Choose your abstractions well, and the rest is simple

Page 23: Java Date and Time - Past and Future

Instants

1379518200000

1379518274000

2013-09-18T15:30Z2013-09-18T18:30+03:00

java.time.Instantjava.time.ZonedDateTimejava.time.OffsetDateTime

java.tim

e.Durati

on

Page 24: Java Date and Time - Past and Future

Civil time java.time.LocalDateTime

java.time.Period

Page 25: Java Date and Time - Past and Future

Q, and, hopefully, A

?

Page 26: Java Date and Time - Past and Future

Links• Java 8 EA https://jdk8.java.net/download.html

– Current version: b106

• Javadocs: http://download.java.net/jdk8/docs/api/

• ISO 8601: http://en.wikipedia.org/wiki/ISO_8601

• Olson TZ aka Zoneinfo – http://www.iana.org/time-zones– http://en.wikipedia.org/wiki/Tz_database

• Stephen Colebourne - author of Joda Time

Page 27: Java Date and Time - Past and Future

Thank you!