date time java 8 (jsr 310)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/1.jpg)
Date Time Java 8(JSR-310)
ilJUG – July 2014By: Eyal Golan
Tech Lead at eBay
![Page 2: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/2.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/3.jpg)
3
Agenda
• Old vs. New• JSR 310• Examples by code• Resources
![Page 4: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/4.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/5.jpg)
5
Example – Issues
![Page 6: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/6.jpg)
6
Example – Issues
Months start at 0
Deprecated
![Page 7: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/7.jpg)
7
Example – Issues
MutableFirst try=1274 , second try=0
Use (Calendar)start.clone();
![Page 8: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/8.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/9.jpg)
9
Example – The New Way
1274
![Page 10: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/10.jpg)
10
Example – The New Way
Simple creation
1274
![Page 11: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/11.jpg)
11
Example – The New Way
Readable DSL
1274
![Page 12: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/12.jpg)
12
Example – The New Way
Immutability
Today 2014-07-29 and next year 2015-07-29
![Page 13: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/13.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/14.jpg)
14
Example – Issues
![Page 15: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/15.jpg)
15
Example – The New Way
Many ways to represent date and time
![Page 16: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/16.jpg)
16
Example – The New Way
Period – human based (date) amount of time
![Page 17: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/17.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/18.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/19.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/20.jpg)
20
Duration and Period
![Page 21: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/21.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/22.jpg)
22
Period – Human Time
![Page 23: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/23.jpg)
JSR 310
![Page 24: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/24.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/25.jpg)
25
Packages
• java.time• java.time.chrono• java.time.format• java.time.temporal• java.time.zone
![Page 26: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/26.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/27.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/28.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/29.jpg)
Code Examples
![Page 30: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/30.jpg)
30
Source Code
https://github.com/eyalgo/java8-datetime
![Page 31: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/31.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/32.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/33.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/34.jpg)
34
The Basics
![Page 35: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/35.jpg)
35
Basic Date and Time
![Page 36: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/36.jpg)
36
Partial Dates & Information
![Page 37: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/37.jpg)
37
Information, Clear API
![Page 38: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/38.jpg)
38
Fluent Operations
![Page 39: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/39.jpg)
39
Time Zone
![Page 40: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/40.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/41.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/42.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/43.jpg)
43
Adjusters
![Page 44: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/44.jpg)
44
Custom Adjusters
2014-08-05
2009-09-01
![Page 45: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/45.jpg)
45
Queries – Retrieve Information
Custom
Custom
![Page 46: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/46.jpg)
46
Custom Queries
![Page 47: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/47.jpg)
47
Custom Queries
![Page 48: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/48.jpg)
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)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/49.jpg)
49
Parsing and Formatting
07**2014--16
![Page 50: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/50.jpg)
50
Legacy Date Time Integration
![Page 51: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/51.jpg)
Resources
![Page 52: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/52.jpg)
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
![Page 53: Date time java 8 (jsr 310)](https://reader038.vdocuments.us/reader038/viewer/2022102900/54b770a44a79595d768b48b2/html5/thumbnails/53.jpg)
THANK YOU
https://www.linkedin.com/in/egolan74
http://eyalgo.com/
@eyalgo_egolan