Professional Documents
Culture Documents
Datetimejava8jsr 310 140730095432 Phpapp02
Datetimejava8jsr 310 140730095432 Phpapp02
(JSR-310)
ilJUG – July 2014
By: Eyal Golan
Tech Lead at eBay
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)
2
Agenda
• Old vs. New
• JSR 310
• Examples by code
• Resources
3
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
4
Example – Issues
5
Example – Issues
6
Example – Issues
Use (Calendar)start.clone();
7
Calendar
• YEAR, MONTH,
DAY_OF_MONTH…
• Fields start at 0
• Calendar is mutable
• Methods (set, add…) change the state of
Calendar
8
Example – The New Way
1274
9
Example – The New Way
1274
10
Example – The New Way
1274
11
Example – The New Way
12
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
13
Example – Issues
14
Example – The New Way
15
Example – The New Way
16
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
17
Example – Old Calculation
now Tue Jul 29 01:48:56 IDT 2014 and later Thu Jan 01 04:20:00 IST 1970
18
Example – Old Calculation
20
Duration – Machine Time
21
Period – Human Time
22
Guidelines
• Clear
• Fluent
• DSL
• Immutability
• Amount of time, different
representations for different cases
• Human (year, month, day)
• Machine time
24
Packages
• java.time
• java.time.chrono
• java.time.format
• java.time.temporal
• java.time.zone
25
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
26
Methods Naming Conventions
of 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
27
Methods Naming Conventions
is Instance Queries state of an object
with Instance Returns a copy of an object with one
changed element
plus / instance Returns a copy of an object with amount of
minus 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
28
Source Code
https://github.com/eyalgo/java8-datetime
30
General
SystemClock[Asia/Jerusalem]
2014-07-29T05:54:23.337
2014-07-29T08:54:23.338
SystemClock[Asia/Jerusalem]
SystemClock[Europe/Berlin]
2014-7-29
31
General
SystemClock[Asia/Jerusalem]
2014-07-29T05:54:23.337
2014-07-29T08:54:23.338
SystemClock[Asia/Jerusalem]
SystemClock[Europe/Berlin]
2014-7-29
32
General
SystemClock[Asia/Jerusalem]
2014-07-29T05:54:23.337
2014-07-29T08:54:23.338
SystemClock[Asia/Jerusalem]
SystemClock[Europe/Berlin]
2014-7-29
33
The Basics
34
Basic Date and Time
35
Partial Dates & Information
36
Information, Clear API
37
Fluent Operations
38
Time Zone
39
Using Zone
40
Instant
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...
41
Adjusters
• Take Temporal value and adjust it
• Pre defined
• firstDayOfMonth
• firstDayOfYear
• lastInMonth
• Look at TemoralAdjusters
• Custom adjusters
42
Adjusters
43
Custom Adjusters
2014-08-05
2009-09-01
44
Queries – Retrieve Information
45
Custom Queries
46
Custom Queries
47
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)
48
Parsing and Formatting
07**2014--16
49
Legacy Date Time Integration
50
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
52
egolan74@gmail.com
https://www.linkedin.com/in/egolan74
http://eyalgo.com/
@eyalgo_egolan