Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Date time java 8 (jsr 310)

3,306 views

Published on

A presentation I did in IL Java User Group about Java 8, DateTime new features. JSR-310

I wanted it to be informative and a good resource and starting point for this cool feature.

Published in: Software

Date time java 8 (jsr 310)

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

×