date time java 8 (jsr 310)

53
Date Time Java 8 (JSR-310) ilJUG – July 2014 By: Eyal Golan Tech Lead at eBay

Upload: eyal-golan

Post on 15-Jan-2015

1.024 views

Category:

Software


3 download

DESCRIPTION

A presentation I did in IL Java User Group about Java 8, DateTime new features. JSR-310 I wanted it to be informative and a good resource and starting point for this cool feature.

TRANSCRIPT

Page 1: Date time java 8 (jsr 310)

Date Time Java 8(JSR-310)

ilJUG – July 2014By: Eyal Golan

Tech Lead at eBay

Page 2: Date time java 8 (jsr 310)

2

A word before starting…

The intent of this presentation is to be basic source and starting point to the new Java Date Time API (JSR-310)

Page 3: Date time java 8 (jsr 310)

3

Agenda

• Old vs. New• JSR 310• Examples by code• Resources

Page 4: Date time java 8 (jsr 310)

4

Old Issues

• Date is not a date, nor time. It’s an instant in time (by epoch)

• Calendar is Date and Time• Month is 0 (zero) based• Mutability

Page 5: Date time java 8 (jsr 310)

5

Example – Issues

Page 6: Date time java 8 (jsr 310)

6

Example – Issues

Months start at 0

Deprecated

Page 7: Date time java 8 (jsr 310)

7

Example – Issues

MutableFirst try=1274 , second try=0

Use (Calendar)start.clone();

Page 8: Date time java 8 (jsr 310)

8

Calendar

• YEAR, MONTH, DAY_OF_MONTH…

• Fields start at 0• Calendar is mutable

• Methods (set, add…) change the state of Calendar

Page 9: Date time java 8 (jsr 310)

9

Example – The New Way

1274

Page 10: Date time java 8 (jsr 310)

10

Example – The New Way

Simple creation

1274

Page 11: Date time java 8 (jsr 310)

11

Example – The New Way

Readable DSL

1274

Page 12: Date time java 8 (jsr 310)

12

Example – The New Way

Immutability

Today 2014-07-29 and next year 2015-07-29

Page 13: Date time java 8 (jsr 310)

13

Modeling

• Different date/time scenarios• Class starts at 12:30• ilJUG is on 29th July• The presentation is 1 hour long• I’ve been working at eBay for 3 years and 6

months

Page 14: Date time java 8 (jsr 310)

14

Example – Issues

Page 15: Date time java 8 (jsr 310)

15

Example – The New Way

Many ways to represent date and time

Page 16: Date time java 8 (jsr 310)

16

Example – The New Way

Period – human based (date) amount of time

Page 17: Date time java 8 (jsr 310)

17

Amount of Time

• How many milliseconds it took between request to response?

• Set time 10 seconds after now• Calculate time that passed between

two events

Page 18: Date time java 8 (jsr 310)

18

Example – Old Calculation

now Tue Jul 29 01:48:56 IDT 2014 and later Thu Jan 01 04:20:00 IST 1970

Page 19: Date time java 8 (jsr 310)

19

Example – Old Calculation

119 days 6 hours 13 minutes 24 seconds 543 ms

Example taken from stackoverflow

Page 20: Date time java 8 (jsr 310)

20

Duration and Period

Page 21: Date time java 8 (jsr 310)

21

Duration – Machine Time

now 2014-07-28T23:17:40.898Z and later 2014-07-29T01:47:40.898Z1502014-07-28T23:17:53.898Z9000000

Page 22: Date time java 8 (jsr 310)

22

Period – Human Time

Page 23: Date time java 8 (jsr 310)

JSR 310

Page 24: Date time java 8 (jsr 310)

24

Guidelines

• Clear• Fluent

• DSL

• Immutability• Amount of time, different representations

for different cases • Human (year, month, day)• Machine time

Page 25: Date time java 8 (jsr 310)

25

Packages

• java.time• java.time.chrono• java.time.format• java.time.temporal• java.time.zone

Page 26: Date time java 8 (jsr 310)

26

Some (not all) Classes

• Temporal – Basic interface for DateTime classes• LocalDate / LocalTime / LocalDateTime …

• Instant• Start of nanoseconds in timeline. Useful for timestamp

• Clock• Allowing Temporal creation with alternate clock

• TemporalAmount – Basic interface for classes that represent amount of time• Duration• Period

Page 27: Date time java 8 (jsr 310)

27

Methods Naming Conventionsof Static Creates an instance with validation

to Instance Converts to another type (truncate fields)

at Instance Combines this object with another (expands)

from Static Converts input parameters to an instance

get Instance Part of the state of the object

Page 28: Date time java 8 (jsr 310)

28

Methods Naming Conventionsis Instance Queries state of an object

with Instance Returns a copy of an object with one changed element

plus / minus

instance Returns a copy of an object with amount of added / subtracted time

parse Static Parses input string to an instance

format Instance Uses formatter to format the object’s values to produce a string

Page 29: Date time java 8 (jsr 310)

Code Examples

Page 30: Date time java 8 (jsr 310)

30

Source Code

https://github.com/eyalgo/java8-datetime

Page 31: Date time java 8 (jsr 310)

31

General

SystemClock[Asia/Jerusalem]2014-07-29T05:54:23.3372014-07-29T08:54:23.338SystemClock[Asia/Jerusalem]SystemClock[Europe/Berlin]2014-7-29

Using clock for time

Page 32: Date time java 8 (jsr 310)

32

General

SystemClock[Asia/Jerusalem]2014-07-29T05:54:23.3372014-07-29T08:54:23.338SystemClock[Asia/Jerusalem]SystemClock[Europe/Berlin]2014-7-29

Zone

Page 33: Date time java 8 (jsr 310)

33

General

SystemClock[Asia/Jerusalem]2014-07-29T05:54:23.3372014-07-29T08:54:23.338SystemClock[Asia/Jerusalem]SystemClock[Europe/Berlin]2014-7-29

Information about Temporal

Page 34: Date time java 8 (jsr 310)

34

The Basics

Page 35: Date time java 8 (jsr 310)

35

Basic Date and Time

Page 36: Date time java 8 (jsr 310)

36

Partial Dates & Information

Page 37: Date time java 8 (jsr 310)

37

Information, Clear API

Page 38: Date time java 8 (jsr 310)

38

Fluent Operations

Page 39: Date time java 8 (jsr 310)

39

Time Zone

Page 40: Date time java 8 (jsr 310)

40

Using Zone

Departure (local time): July 16 2014 23:00Arrival (local time): July 17 2014 03:51

Set the zones

Arriving at different local time

Departure at…

Page 41: Date time java 8 (jsr 310)

41

Instant

Closest to Date…

From Javadoc:"This class models a single instantaneous point on the time-line.This might be used to record event time-stamps in the application...

"...number of seconds that can be held in a long. This is greater than the current estimated age of the universe...

"...The instant is stored to nanosecond resolution..."

Page 42: Date time java 8 (jsr 310)

42

Adjusters

• Take Temporal value and adjust it• Pre defined

• firstDayOfMonth• firstDayOfYear• lastInMonth

• Look at TemoralAdjusters• Custom adjusters

Page 43: Date time java 8 (jsr 310)

43

Adjusters

Page 44: Date time java 8 (jsr 310)

44

Custom Adjusters

2014-08-05

2009-09-01

Page 45: Date time java 8 (jsr 310)

45

Queries – Retrieve Information

Custom

Custom

Page 46: Date time java 8 (jsr 310)

46

Custom Queries

Page 47: Date time java 8 (jsr 310)

47

Custom Queries

Page 48: Date time java 8 (jsr 310)

48

Parsing and Formatting

• DateTimeFormatter• Many pre defined formats• http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#predefined

• Immutable• DateTimeFormatterBuilder• All classes use the same way• DateTimeParseException (runtime)

Page 49: Date time java 8 (jsr 310)

49

Parsing and Formatting

07**2014--16

Page 50: Date time java 8 (jsr 310)

50

Legacy Date Time Integration

Page 51: Date time java 8 (jsr 310)

Resources

Page 52: Date time java 8 (jsr 310)

52

https://github.com/eyalgo/java8-datetime

http://docs.oracle.com/javase/tutorial/datetime/iso/index.html

http://docs.oracle.com/javase/tutorial/datetime/index.html

https://jcp.org/en/jsr/detail?id=310

http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html

http://www.mscharhag.com/2014/02/java-8-datetime-api.html

http://geekmonkey.org/articles/24-a-new-date-and-time-api-for-jdk-8

https://today.java.net/pub/a/today/2008/09/18/jsr-310-new-java-date-time-api.html