Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

Java 8 Date-Time API

Core of Java Date-Time API.

Examples can be downloaded from https://github.com/andysundar/JDK8

  • Inicia sesión para ver los comentarios

Java 8 Date-Time API

  1. 1. Date-Time (JSR-310) By – Anindya Bandopadhyay Java SE 8 isn't the same Java we know.
  2. 2. Date Time (JSR-310) Java SE 8 isn't the same Java we know.
  3. 3. Why another set of Date Time ? • Date and Calendar class didn’t have adequate support. • Date and Calendar objects are not thread safe. • Date and Calendar didn’t allow different calendar system like Japanese, Thai Buddhist and other calendars. Java SE 8 isn't the same Java we know.
  4. 4. What is Java 8 Date-Time offering ? (JSR-310) Java SE 8 isn't the same Java we know.
  5. 5. Object are immutable as a result they are thread safe. Java SE 8 isn't the same Java we know.
  6. 6. API’s are more organize and error free. Java SE 8 isn't the same Java we know.
  7. 7. It allow different calendar system like Japanese, Thai Buddhist and other calendars. Java SE 8 isn't the same Java we know.
  8. 8. Possible to write custom query against new Date-Time Java SE 8 isn't the same Java we know.
  9. 9. It provide accuracy at the range of nano second. Java SE 8 isn't the same Java we know.
  10. 10. Basic understanding of Date-Time • In Java 8 Date-Time have representation of two type. • One way of represents Date-Time is in human terms such as year, month, day, hour, minute and second. • Other way is in machine terms, measures time continuously along a timeline from an origin, called the epoch, in nanosecond resolution. Java SE 8 isn't the same Java we know.
  11. 11. What is epoch ? • In the fields of chronology and periodization, an epoch is an instant in time chosen as the origin of a particular era. The "epoch" then serves as a reference point from which time is measured. • For us it is Unix Time [Epoch Time]. Java SE 8 isn't the same Java we know.
  12. 12. What is Unix time ? • Unix time (also known as POSIX time or Epoch time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970,[1][note 1] not counting leap seconds. Java SE 8 isn't the same Java we know. 1st Jan 1970 Midnight Current UTC time
  13. 13. Problem because of epoch time Java SE 8 isn't the same Java we know.
  14. 14. Java SE 8 isn't the same Java we know.
  15. 15. Something interesting Source of information is Independent. http://www.independent.co.uk/life- style/gadgets-and-tech/news/facebook-46- years-notifications-why-social-network-told- users-they-had-been-friends-for-decades-and- a6792661.html Java SE 8 isn't the same Java we know.
  16. 16. Overview of java.time package • java.time - the base package containing the value objects • java.time.chrono -provides access to different calendar systems [like Thai Buddhist ] • java.time.format - allows date and time to be formatted and parsed • java.time.temporal - the low level framework and extended features • java.time.zone - support classes for time-zones Java SE 8 isn't the same Java we know.
  17. 17. LocalDate Java SE 8 isn't the same Java we know. This object only contain date component.
  18. 18. It is consist of Day, Month, Year. Java SE 8 isn't the same Java we know.
  19. 19. What is today date ? • LocalDate today = LocalDate.now(); • System.out.println("Today's Date : "+ today); • Output :- “Today's Date : 2016-01-12” Java SE 8 isn't the same Java we know.
  20. 20. What is previous date ? • LocalDate today = LocalDate.now(); • System.out.println("Previous Date : "+ today.minusDays(1)); • Output :- “Previous Date : 2016-01-11” Java SE 8 isn't the same Java we know.
  21. 21. What is next date ? • LocalDate today = LocalDate.now(); • System.out.println("Next Date : "+ today.plusDays(1)); • Output :- “Next Date : 2016-01-13” Java SE 8 isn't the same Java we know.
  22. 22. What is the length of year ? • LocalDate today = LocalDate.now(); • System.out.printf("%s days in %s n",today.lengthOfYear(),today.getYear()); • Output :- “366 days in 2016” Java SE 8 isn't the same Java we know.
  23. 23. Is leap year ? • LocalDate today = LocalDate.now(); • System.out.printf("%s is leap year ? %s n",today.get(ChronoField.YEAR), today.isLeapYear()); • Output :- “2016 is leap year ? true” Java SE 8 isn't the same Java we know.
  24. 24. LocalTime • This object only contain time component. • It is consist of hour, minute and second. • E.g. What is the time now ? “05:40 AM” Java SE 8 isn't the same Java we know.
  25. 25. What is the time ‘now’ ? • LocalTime timeNow = LocalTime.now(); • System.out.println("Time now : "+ timeNow); Java SE 8 isn't the same Java we know.
  26. 26. Simply add an hour to current time • LocalTime timeNow = LocalTime.now(); • System.out.println("Meeting in next hour : "+ timeNow.plus(1, ChronoUnit.HOURS)); Java SE 8 isn't the same Java we know.
  27. 27. Only take out minutes • LocalTime timeNow = LocalTime.now(); • System.out.println("Only upto minutes : "+ timeNow.truncatedTo(ChronoUnit.MINUTES)); Java SE 8 isn't the same Java we know.
  28. 28. LocalDateTime • This object contain date and time components. • It is consist of year,month,day,hour, minute and second. Java SE 8 isn't the same Java we know.
  29. 29. LocalDate, LocalTime and LocalDateTime objects doesn’t contain information regarding time-zone. As it is ‘Local’. Java SE 8 isn't the same Java we know.
  30. 30. Time Zone • Numerous of Time-Zones are covered. • These Time-Zones are govern by political rules. • These rules are complex and might frequently change. • E.g. In Brazil before 2008, there were no fixed start and end dates for DST. Java SE 8 isn't the same Java we know.
  31. 31. Multiple group provide rules • Time Zone DB • Windows • International Air Transport Association (IATA) Note :- With JDK update time-zone database get updated locally. Java SE 8 isn't the same Java we know.
  32. 32. Zone Id • A ZoneId is used to identify the rules used to convert between an Instant and a LocalDateTime. • It is replacement for TimeZone class. • Eg. “Asia/Calcutta” , “Etc/GMT-1” Java SE 8 isn't the same Java we know.
  33. 33. ZoneOffset • This object represent the time zone difference from Greenwich/UTC. • This is usually a fixed number of hours and minutes. Java SE 8 isn't the same Java we know.
  34. 34. ZoneDateTime • It contain date and time along with time zone details. Java SE 8 isn't the same Java we know.
  35. 35. Year • This object contain only Year information. Java SE 8 isn't the same Java we know. Which year is this ? 2016
  36. 36. YearMonth • This object contains year and month component only. Java SE 8 isn't the same Java we know.
  37. 37. Credit Card expire date : 2019-11 Java SE 8 isn't the same Java we know.
  38. 38. MonthDay • This object represent month and day component only. Java SE 8 isn't the same Java we know.
  39. 39. AMEX Card billing cycle : --02-22 Java SE 8 isn't the same Java we know.
  40. 40. OffsetTime • This object represents time and offset from UTC/GMT. Java SE 8 isn't the same Java we know. 22:52:58.868+05:30
  41. 41. OffsetDateTime • This object represents date , time and offset from UTC/GMT. Java SE 8 isn't the same Java we know. 2016-01-17T22:42:50.541+05:30
  42. 42. Instance • This object represent timestamp and it is used where we need to record any event timestamp in the application. Java SE 8 isn't the same Java we know.
  43. 43. Get the time in Epoch seconds : 1453812691 Java SE 8 isn't the same Java we know.
  44. 44. Clock • This object return current system timestamp. • This object allows to inject a real or fake clock into other java.time objects. • This class can be used for unit testing. Java SE 8 isn't the same Java we know.
  45. 45. Output is in next slide Java SE 8 isn't the same Java we know.
  46. 46. • Get an Instant from clock object =>2016-01-26T13:06:35.776Z • Create an Instant from the reference of clock object =>2016- 01-26T13:06:35.792Z • Create an LocalDateTime from the reference of clock object =>2016-01-26T18:36:35.792 • Create an ZonedDateTime from the reference of clock object =>2016-01-26T18:36:35.792+05:30[Asia/Calcutta] • Create an OffsetDateTime from the reference of clock object =>2016-01-26T18:36:35.792+05:30 Java SE 8 isn't the same Java we know.
  47. 47. toString() method return string in ISO 8601 format Java SE 8 isn't the same Java we know.
  48. 48. Commonly Used Methods Java SE 8 isn't the same Java we know. Prefix Method Type Use of static factory Creates an instance where the factory is primarily validating the input parameters, not converting them. from static factory Converts the input parameters to an instance of the target class, which may involve losing information from the input. parse static factory Parses the input string to produce an instance of the target class. format instance Uses the specified formatter to format the values in the temporal object to produce a string. get instance Returns a part of the state of the target object. is instance Queries the state of the target object. with instance Returns a copy of the target object with one element changed; this is the immutable equivalent to a set method on a JavaBean. plus instance Returns a copy of the target object with an amount of time added. minus instance Returns a copy of the target object with an amount of time subtracted. to instance Converts this object to another type. at instance Combines this object with another.
  49. 49. Formatting and Parsing • format method coverts a temporal based object to a string representation using the specified format. • parse method covert a string date/time to temporal based object. Java SE 8 isn't the same Java we know.
  50. 50. DateTimeFormatter • This class is primarily responsible for formatting. • DateTimeFormatterBuilder class allow a DateTimeFormatter to be created. • Formatter can be created with desired Locale, Chronology, ZoneId and DecimalStyle. Java SE 8 isn't the same Java we know.
  51. 51. Output:- Date formatting -> Dec 20 2001 4 AD Time formatting -> 11:23 PM Java SE 8 isn't the same Java we know. Date-Time Formatting
  52. 52. Date-Time Parsing Java SE 8 isn't the same Java we know. Output :- Date parsing -> 2001-12-20 Time parsing -> 23:23
  53. 53. Temporal • java.time.temporal package support date, time code and in particular date time calculations. Java SE 8 isn't the same Java we know.
  54. 54. Temporal Type • Application code should NOT declare variable type as temporal interface. Unless application code doing something as very basic level. (E.g. Doing calculation) Java SE 8 isn't the same Java we know.
  55. 55. TemporalAdjuster • This interface provide method to take a temporal value and return an adjuster. • To create custom adjuster need to implement TemporalAdjuster interface. • Temporal adjustInto(Temporal temporal) Java SE 8 isn't the same Java we know.
  56. 56. TemporalAdjusters (Plural) • This class provide predefine adjusters. • Eg.  first day of month/year, last day of month/year, next Sunday Java SE 8 isn't the same Java we know.
  57. 57. Custom Adjuster Java SE 8 isn't the same Java we know. Output :- 2016-02-28
  58. 58. TemporalQuery<R> • This interface can be used to retrieve information from a temporal based object. • R queryFrom(TemporalAccessor temporal) Java SE 8 isn't the same Java we know.
  59. 59. Java SE 8 isn't the same Java we know. Output :- Is holiday ? -> true Custom Query
  60. 60. TemporalQueries (Plural) • This class provide predefine queries. • E.g.  offset , precision query Java SE 8 isn't the same Java we know.
  61. 61. Java SE 8 isn't the same Java we know. Output
  62. 62. Period • Period represent amount of time in terms of years, months and days. Java SE 8 isn't the same Java we know.
  63. 63. How many days passed after taken 2016 new year resolution ? P16D Java SE 8 isn't the same Java we know.
  64. 64. Duration • Duration represent amount of time in terms of seconds and nano-seconds. Java SE 8 isn't the same Java we know.
  65. 65. Duration between two time : PT0.1S Java SE 8 isn't the same Java we know.
  66. 66. Converting to a Non-ISO-Based Date Java SE 8 isn't the same Java we know. Output :-
  67. 67. Interoperability with Legacy Code • Calendar.toInstant() converts the Calendar object to an Instant. • GregorianCalendar.toZonedDateTime() converts a GregorianCalendar instance to a ZonedDateTime. • GregorianCalendar.from(ZonedDateTime) creates a GregorianCalendar object using the default locale from a ZonedDateTime instance. • Date.from(Instant) creates a Date object from an Instant. • Date.toInstant() converts a Date object to an Instant. • TimeZone.toZoneId() converts a TimeZone object to a ZoneId. Java SE 8 isn't the same Java we know.
  68. 68. Look time piece makers .. How much you can make Java SE 8 isn't the same Java we know.

×