2. About
me
Oleg
Tsal-‐Tsalko
Lead
So:ware
Engineer
at
EPAM
Systems.
Speaker,
acEve
member
of
Kiev
JUG.
ParEcipate
in
different
educaEonal
iniEaEves
and
JCP/AdoptJSR
programs.
3. Overview
• JSR-‐310:
New
Date
&
Time
API
• Replaces
old
ambiguous
java.uEl.Date,
Calendar,
TimeZone,
DateFormat
classes
• More
fluent/simple/clean
API
• Immutable
classes
• Using
Java8
features
including
lambdas
• Precise
separaEon
of
concepts
4. Range
of
types
• LocalDate
–
a
date
only
• LocalTime
–
a
Eme
only
• LocalDateTime
–
date
with
Eme
• ZonedDateTime
–
date
with
Eme
in
Eme
zone
• And
more…
5. LocalDate
Stores
year-‐month-‐day
Use
cases:
birthdays,
start/end
dates,
holidays
dates
LocalDate
current
=
LocalDate.now();
LocalDate
date
=
LocalDate.of(2014,
Month.AUGUST,
10);
If
(current.isA:er(date))…
boolean
leap
=
date.isLeapYear();
int
monthLength
=
date.lengthOfMonth();
6. Dates
manipulaEon
Because
LocalDate
is
immutable
we
have
plus/minus/with
methods
instead
add/set
methods:
date
=
date.plusMonth(1).minusDays(5);
date
=
date.withDayOfMonth(1);
date
=
date.with(Month.SEPTEMBER);
7. Using
adjusters
For
more
complex
dates
manipulaEons
we
use
many
predefined
TemporalAdjusters
or
create
custom
ones:
date
=
date.with(TemporalAdjusters.lastDayOfMonth());
date
=
date.with(next(TUESDAY))
8. LocalTime
Stores
hour-‐minute-‐second-‐nanosecond
Use
cases:
shop
openning
hous,
clock
alarms,
etc.
LocalTime
current
=
LocalTime.now();
LocalTime
Eme
=
LocalTime.of(13,30);
If
(current.isBefore(Eme))
…
Eme
=
Eme.plusHours(4).plusMinutes(10).minusSeconds(30);
Eme
=
Eme.truncatedTo(SECONDS);
10. TimeZones
We
have
numerous
of
TimeZones
governed
by
poliIcal
rules
which
someEmes
complex
and
might
change
frequently.
If
you
can
avoid
using
TimeZones
–
do
it!
11. TimeZone
classes
in
Java
8
• ZoneId
–
replacement
for
TimeZone
class
(e.g.
“Europe/London”,
“Europe/Kiev”)
• ZoneOffset
–
represenEng
offset
from
UTC
Eme
• ZoneRules
–
behind
the
scenes
class
which
defines
Eme
zone
rules
• ZonedDateTime
–
main
date/Eme
class
which
is
aware
of
Eme
zones
12. ZonedDateTime
Internaly
stores
LocalDateTime,
ZoneId
and
ZoneOffset
and
is
closest
equivalent
to
java.u2l.GregorianCalendar.
zone
=
ZoneId.of(“Europe/London”);
zonedDateTime
=
ZonedDateTime.of(2014,
AUGUST,
10,
13,
30,
0,
0,
zone);
zonedDateTime.plusDays(1).minusMinutes(30);
Takes
care
of
‘daylight
savings’
and
no
excepEons
thrown
in
ambigue
cases
instead
act
on
best
effort
basis
13. Calendar
systems
• All
main
classes
use
ISO
calendar
system
• Other
calendar
systems
(Hijrah,
Japanese,
Minguo,
ThaiBuddist,
etc.)
also
supported
however
not
at
the
same
degree
and
might
be
complicated
to
use.
• Good
thong
is
that
diff
calendar
systems
separated
from
each
other
• Main
interfaces
to
be
implemented
for
new
calendar
systems
are:
Cronology
and
ChronoLocalDate
14. Power
of
abstracEon
New
API
is
very
flexible
because
it
based
on
number
of
abstracEons
at
it’s
bopom:
• Temporal
–
parent
class
for
all
date/Eme
objects
which
defines
mutaEon
operaEon
for
them
such
as
plus/
minus/with
• TemporalAdjuster
–
funcEonal
interface
which
responsible
for
mutaEng
Temporal
objects
• TemporalField
–
represents
parts/fields
of
date/Eme
objects
such
as
(DAY_OF_WEEK,
MONTH,
etc.)
• TemporalUnit
–
represents
type
of
date/Eme
values
such
as
(MINUTES,
DAYS,
YEARS,
etc.)
• TemporalAmount
–
class
which
represents
amount
of
Eme
15. DuraEon
Time-‐based
amount
of
Eme
in
hours,
minutes,
seconds
or
nanoseconds.
Use
cases:
Emeouts
duraEon
=
DuraEon.ofHours(6);
duraEon
=
duraEon.mulEpliedBy(3);
duraEon
=
duraEon.plusMinutes(30);
date
=
LocalDateTime.now();
Date.plus(duraEon);
16. Period
Date-‐based
amount
of
Eme
in
years,
months,
days.
Use
cases:
length
of
holiday,
length
of
trip
period
=
Period.ofMonth(9);
period
=
period.plusDays(6);