Java 8 date & time

1,047 views
818 views

Published on

Presentation from Java 8 Date and Time hacking event on Kiev JUG meet up during EPAM IT Week

Published in: Software
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,047
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
24
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Java 8 date & time

  1. 1. Speaker:  Oleg  Tsal-­‐Tsalko(@tsaltsol)   Java  8  Date  &  Time  API   (based  on  Stephen  Colebourne  Java  8  launch  talk)  
  2. 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. 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. 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. 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. 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. 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. 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);  
  9. 9. LocalDateTime   Basically  combinaEon  of  LocalDate  and  LocalTime.     All  methods  are  similar  to  LocalDate  and  LocalTime…  
  10. 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. 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. 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. 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. 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. 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. 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);  
  17. 17. Links   •  hpps://today.java.net/pub/a/today/2008/09/18/ jsr-­‐310-­‐new-­‐java-­‐date-­‐Eme-­‐api.html   •  hpp://blog.joda.org/2009/11/why-­‐jsr-­‐310-­‐isn-­‐ joda-­‐Eme_4941.html   •  hpps://blogs.oracle.com/thejavatutorials/entry/ javaone_2013_jdk_8_date   •  hpp://vimeo.com/87157763   •  hpp://parleys.com/play/ 52508380e4b0c4f11ec57665/about   •  hpp://www.oracle.com/events/us/en/java8/ index.html  
  18. 18. Thank  you!   Oleg  Tsal-­‐Tsalko   Email:  oleg.tsalko@gmail.com   Twiper:  @tsaltsol        

×