Introduction to Java 8 Date Time API
Nadeesh T V
ORACLE India Pvt Ltd
19 Dec 2015
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
Outline
1 Why do we need a new API?
2 JSR 310
3 Chronology
4 TimeZone
5 Summary
6 What to look for in java 9
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
Drawbacks of existing APIutil.Date, util.Calendar ..
What is the output of following code?
C a l e n d a r c a l = new G r e g o r i a n C a l e n d a r (1970 , 12 , 19 , 11 , 3 0 ) ;
SimpleDateFormat dateFormat = new SimpleDateFormat ( ” yyyy−MM−dd HH:mm: s s ” ) ;
System . out . p r i n t l n ( dateFormat . fo rmat ( c a l . getTime ( ) ) ) ;
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
Drawbacks of existing APIutil.Date, util.Calendar ..
What is the output of following code?
C a l e n d a r c a l = new G r e g o r i a n C a l e n d a r (1970 , 12 , 19 , 11 , 3 0 ) ;
SimpleDateFormat dateFormat = new SimpleDateFormat ( ” yyyy−MM−dd HH:mm: s s ” ) ;
System . out . p r i n t l n ( dateFormat . fo rmat ( c a l . getTime ( ) ) ) ;
Expected: 1970-12-19 11:30:00 Actual : 1971-01-19 11:30:00
Months are zero indexed :)
How to model Date without time component? eg: 19 Dec 2015
How to model Time without a date? eg: 10.30
How to model Month without a day? eg: credit card expiry date
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
Drawbacks of existing APIutil.Date, util.Calendar ..
How to model Duration? eg: 2H 15 minute
How to find number of days between 2 dates?
Both Date and Calendar are mutable
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
JSR 310 : Date Time API
Started in 2007
Feature Completed in 2013
JSR 310 is not joda time
Adopted from joda time
Goals
Immutable
Fluent and Clear
Pluggable or Extensible
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
JSR 310 : LocalDate
Stores year-month-day
19 Dec 2015
Start day, End day etc
date1 = Loca lDate . now ( ) ;
date2 = Loca lDate . o f (2015 , 12 , 3 1 ) ;
date2 . i s A f t e r ( date1 ) ;
date2 . i s L e a p Y e a r ( ) ;
date2 . lengthOfMonth ( ) ;
Manipulation - yet immutable
date2 . p l u s D a y s ( 3 ) ;
date2 . minusMonths ( 2 ) ;
date2 . withDayOfMonth ( 1 2 ) ;
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
JSR 310 : LocalDate
More complex alteration use TemporalAdjuster
Eg: Change to last day of month
Change to next Tuesday
Change to 3rdFriDay
TemporalAdjuster - yet immutable
date2 . w i t h ( T e m p o r a l A d j u s t e r s . f i r s tDayOfMonth ( ) ) ;
date2 . w i t h ( T e m p o r a l A d j u s t e r s . n e x t ( DayOfWeek .TUESDAY ) ) ;
date2 . w i t h ( T e m p o r a l A d j u s t e r s . n e x t (3 r d F r i d a y ) ;
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
JSR 310 : LocalTime
Storeshour-minute-seconds-nano
10:30
Wall clock time, openingtime etc
t ime1 = LocalTime . now ( ) ;
t ime2 = LocalTime . o f (2015 , 12 , 3 1 ) ;
t ime2 . i s A f t e r ( t ime1 ) ;
t ime2 . getHour ( ) ;
Manipulation - yet immutable
t ime . withNano ( 0 ) ;
t ime . p l u s H o u r s ( 2 ) ;
t ime . t r u n c a t e d T o ( ChronoUnit . SECONDS)
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
JSR 310 : LocalDateTime
Stores LocalDate and LocalTime
19 Dec 2015 10:30
Wall clock time, opening time etc
Manipulation - yet immutable
dateTime1 = LocalDateTime . now ( ) ;
dateTime2 = LocalDateTime . o f (2015 , 12 , 3 1 , 1 1 , 3 0 , 1 2 ) ;
dateTime1 . i s A f t e r ( dateTime12 ) ;
dateTime1 . p l u s D a y s ( 3 ) . p l u s H o u r s ( 2 ) ;
dateTime1 . w i t h ( T e m p o r a l A d j u s t e r s . f i r s tDayOfMonth ( ) ) ;
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
Chronology
Chronlogy - Calendar System
Currently support 5 Chronologies
ISo 8601, Hijrah, Japanese, Minguo, ThaiBudhist
Pluggable
ChronloLocalDate, ChronoLocalTime, ChronoLocalDateTime etc
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
TimeZone
World is divided into different time zones
JSR 310 uses tzdb
To figure out TimeOffset, DST rules etc
3 Classes - ZoneOffset, ZoneId, ZoneRules
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
JSR 310 : ZonedDateTime
Stores LocalDateTime, ZoneOffset, ZoneId
19 Dec 2015 10:30 +05:30[Asia/Calcutta]
Manipulation - yet immutable
ZonedDateTime . now ( ) ;
ZoneId zone= ZoneId . o f ( ” A s i a / C a l c u t t a ” ) ;
ZonedDateTime . o f ( l o c a l d a t e T i m e , zone ) ;
z d t . p l u s D a y s ( 3 ) . p l u s H o u r s ( 2 ) ;
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
JSR 310 : Instant
Represent instantaneous point in time
Stores nano seconds from 1970-1-1Z
Equivalent to Machine Time
Manipulation - yet immutable
I n s t a n t i n s t a n t 1 = I n s t a n t . now ( ) ;
i n s t a n t 1 . p l u s M i l l i s ( 2 0 ) ;
i n s t a n t 1 . i s A f t e r ( i n s t a n t 2 ) ;
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
JSR 310 : Duration
Represent amount of time
Stores seconds and nano seconds
Eg: 24.3 seconds
Manipulation - yet immutable
D u r a t i o n dur = D u r a t i o n . o f S e c o n d s ( 1 2 , 2 1 ) ;
dur . p l u s H o u r s ( 2 ) ;
dur . m u l t i p l i e d B y ( 1 0 ) ;
LocalDateTime . now ( ) . p l u s ( dur ) ;
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
JSR 310 : Period
date based amount
years , months , days
Eg: 2 year 1 month
Manipulation - yet immutable
P e r i o d p e r i o d = P e r i o d . o f ( y e a r s , onths , days ) ;
p e r i o d . p l u s D a y s ( 2 ) ;
LocalDateTime . now ( ) . p l u s ( p e r i o d ) ;
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
JSR 310 : Class Summary
LocalDate 2015-12-19
LocalTime 11:30
LocalDateTime 2015-12-19T11:30
ZonedDateTime 2015-12-19T11:30+5:30 Asia/Calcutta
Instant 12202 seconds
Duration PT24.3 seconds
Period PT1Y2M
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
Convertions
// Loca lDate / LocalTime <−> LocalDateTimeLoca lDate d a t e = Loca lDate . now ( ) ;
LocalTime t ime = LocalTime . now ( ) ;
LocalDateTime dateTimeFromDateAndTime = LocalDateTime . o f ( date , t ime ) ;
Loca lDate dateFromDateTime = LocalDateTime . now ( ) . t o L o c a l D a t e ( ) ;
LocalTime timeFromDateTime = LocalDateTime . now ( ) . toLoca lT ime ( ) ;
// I n s t a n t <−> LocalDateTimeI n s t a n t i n s t a n t = I n s t a n t . now ( ) ;
LocalDateTime dateTimeFromInstant = LocalDateTime .o f I n s t a n t ( i n s t a n t , ZoneId . o f ( ” America / L o s A n g e l e s ” ) ) ;
I n s t a n t instantFromDateTime = LocalDateTime . now ( ) .t o I n s t a n t ( Z o n e O f f s e t . o fHours ( −2));
// conv e r t o l d date / c a l e n d a r / t imezone c l a s s e sI n s t a n t i n s t a n t F r o m D a t e = new Date ( ) . t o I n s t a n t ( ) ;
I n s t a n t i n s t a n t F r o m C a l e n d a r = C a l e n d a r . g e t I n s t a n c e ( ) . t o I n s t a n t ( ) ;
ZoneId z o n e I d = TimeZone . g e t D e f a u l t ( ) . to Z on e Id ( ) ;
ZonedDateTime zonedDateTimeFromGregor ianCalendar = new G r e g o r i a n C a l e n d a r ( ). toZonedDateTime ( ) ;
// conv e r t to o l d c l a s s e sDate d a t e F r o m I n s t a n t = Date . from ( I n s t a n t . now ( ) ) ;
TimeZone timeZone = TimeZone . getTimeZone ( ZoneId . o f ( ” America / L o s A n g e l e s ” ) ) ;
G r e g o r i a n C a l e n d a r g r e g o r i a n C a l e n d a r = G r e g o r i a n C a l e n d a r . from ( ZonedDateTime . now ( ) ) ;
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
What to look for in java 9
Duration - toHourParts(),toMinutesPart(),dividedBY(Duarion)
LocalDate - ofInstant(Instant,Zone), toEpochSecond()
Julian Chronolgy(may be)
Additional patterns in DateTimeFormatter
Optimize implemenations of certain method implmentaion inLocalDate, LocalTime
etc ...
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
References
1. http://www.threeten.org
2. https://jcp.org/en/jsr/detail?id=310
3.www.javacodegeeks.com/2014/03/a-deeper-look-into-the-java-8-date-and-time-api.html
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
Thank you
Bangalore JUG Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.