date time java 8 (jsr 310)

Post on 15-Jan-2015

1.019 Views

Category:

Software

3 Downloads

Preview:

Click to see full reader

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

Date Time Java 8(JSR-310)

ilJUG – July 2014By: Eyal Golan

Tech Lead at eBay

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)

3

Agenda

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

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

5

Example – Issues

6

Example – Issues

Months start at 0

Deprecated

7

Example – Issues

MutableFirst try=1274 , second try=0

Use (Calendar)start.clone();

8

Calendar

• YEAR, MONTH, DAY_OF_MONTH…

• Fields start at 0• Calendar is mutable

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

9

Example – The New Way

1274

10

Example – The New Way

Simple creation

1274

11

Example – The New Way

Readable DSL

1274

12

Example – The New Way

Immutability

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

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

14

Example – Issues

15

Example – The New Way

Many ways to represent date and time

16

Example – The New Way

Period – human based (date) amount of time

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

18

Example – Old Calculation

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

19

Example – Old Calculation

119 days 6 hours 13 minutes 24 seconds 543 ms

Example taken from stackoverflow

20

Duration and Period

21

Duration – Machine Time

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

22

Period – Human Time

JSR 310

24

Guidelines

• Clear• Fluent

• DSL

• Immutability• Amount of time, different representations

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

25

Packages

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

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

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

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

Code Examples

30

Source Code

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

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

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

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

34

The Basics

35

Basic Date and Time

36

Partial Dates & Information

37

Information, Clear API

38

Fluent Operations

39

Time Zone

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…

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..."

42

Adjusters

• Take Temporal value and adjust it• Pre defined

• firstDayOfMonth• firstDayOfYear• lastInMonth

• Look at TemoralAdjusters• Custom adjusters

43

Adjusters

44

Custom Adjusters

2014-08-05

2009-09-01

45

Queries – Retrieve Information

Custom

Custom

46

Custom Queries

47

Custom Queries

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)

49

Parsing and Formatting

07**2014--16

50

Legacy Date Time Integration

Resources

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

THANK YOU

Q?Aegolan74@gmail.com

https://www.linkedin.com/in/egolan74

http://eyalgo.com/

@eyalgo_egolan

top related