Java Date and Time - Past and Future

Preview:

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

JavaDate and Time

Past and Future

Iulian DogariuNess Technologies

Iași Java User Group1379518200000

APIs that make me happy

Timestamp

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

-- Bill Gates,after seeing the JDK 1.0 Javadocs

(well, not really)

java.util.Date

java.util.Calendar

POLA !

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

Instants

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

Civil time

Instants ↔ Civil time

Let’s Practice !

Java 8 Time vs Joda Time

We love .NET too

http://nodatime.org/

Java 8 Time – Recap

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

• Choose your abstractions well, and the rest is simple

Instants

1379518200000

1379518274000

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

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

java.tim

e.Durati

on

Civil time java.time.LocalDateTime

java.time.Period

Q, and, hopefully, A

?

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

Thank you!

Recommended