Introduction to Java 8 Date Time API Nadeesh T V ORACLE India Pvt Ltd 19 Dec 2015
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
Drawbacks of existing API util.date, util.calendar.. What is the output of following code? Calendar c a l = new GregorianCalendar (1970, 12, 19, 11, 30); SimpleDateFormat dateformat = new SimpleDateFormat ( yyyy MM dd HH:mm: ss ) ; System. out. p r i n t l n ( dateformat. format ( c a l. gettime ( ) ) ) ;
Drawbacks of existing API util.date, util.calendar.. What is the output of following code? Calendar c a l = new GregorianCalendar (1970, 12, 19, 11, 30); SimpleDateFormat dateformat = new SimpleDateFormat ( yyyy MM dd HH:mm: ss ) ; System. out. p r i n t l n ( dateformat. format ( 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
Drawbacks of existing API util.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
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
JSR 310 : LocalDate Stores year-month-day 19 Dec 2015 Start day, End day etc date1 = L o caldate. now ( ) ; date2 = LocalDate. of (2015, 12, 31); 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 ( 12);
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. with ( TemporalAdjusters. firstdayofmonth ( ) ) ; date2. with ( TemporalAdjusters. next ( DayOfWeek.TUESDAY ) ) ; date2. with ( TemporalAdjusters. next (3 rdfriday ) ;
JSR 310 : LocalTime Stores hour-minute-seconds-nano 10:30 Wall clock time, opening time etc time1 = LocalTime. now ( ) ; time2 = LocalTime. of (2015, 12, 31); time2. i s A f t e r ( time1 ) ; time2. gethour ( ) ; Manipulation - yet immutable time. withnano ( 0 ) ; time. p l u s H o u r s ( 2 ) ; time. truncatedto ( ChronoUnit.SECONDS)
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. with ( TemporalAdjusters. firstdayofmonth ( ) ) ;
Chronology Chronlogy - Calendar System Currently support 5 Chronologies ISo 8601, Hijrah, Japanese, Minguo, ThaiBudhist Pluggable ChronloLocalDate, ChronoLocalTime, ChronoLocalDateTime etc
TimeZone World is divided into different time zones JSR 310 uses tzdb To figure out TimeOffset, DST rules etc 3 Classes - ZoneOffset, ZoneId, ZoneRules
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 ) ;
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 ) ;
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 ) ;
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 ) ;
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
Convertions // LocalDate / LocalTime < > LocalDateTime LocalDate date = LocalDate. now ( ) ; LocalTime time = LocalTime. now ( ) ; LocalDateTime datetimefromdateandtime = LocalDateTime. o f ( date, time ) ; LocalDate datefromdatetime = LocalDateTime. now ( ). tolocaldate ( ) ; LocalTime timefromdatetime = LocalDateTime. now ( ). tolocaltime ( ) ; // I n s t a n t < > LocalDateTime I 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)); // c o n v e r t o l d d a t e / c a l e n d a r / t i m e z one c l a s s e s I 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 zoneid = TimeZone. getdefault ( ). tozoneid ( ) ; ZonedDateTime zoneddatetimefromgregoriancalendar = new Gr ego ria nc ale nd ar ( ). tozoneddatetime ( ) ; // c o n v e r t to o l d c l a s s e s Date d a t e F r o m I n s t a n t = Date. Bangalore from ( I n sjug t a n t. now ( 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 in LocalDate, LocalTime etc...
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
Thank you