KEMBAR78
Java 8 Date-Time API | PPTX
Date-Time
(JSR-310)
By – Anindya Bandopadhyay
Java SE 8 isn't the same Java we know.
Date Time (JSR-310)
Java SE 8 isn't the same Java we know.
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.
What is Java 8 Date-Time offering ?
(JSR-310)
Java SE 8 isn't the same Java we know.
Object are immutable as a result they
are thread safe.
Java SE 8 isn't the same Java we know.
API’s are more organize and error free.
Java SE 8 isn't the same Java we know.
It allow different calendar system like
Japanese, Thai Buddhist and other
calendars.
Java SE 8 isn't the same Java we know.
Possible to write custom query against
new Date-Time
Java SE 8 isn't the same Java we know.
It provide accuracy at the range of
nano second.
Java SE 8 isn't the same Java we know.
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.
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.
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
Problem because of epoch time
Java SE 8 isn't the same Java we know.
Java SE 8 isn't the same Java we know.
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.
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.
LocalDate
Java SE 8 isn't the same Java we know.
This object only contain date
component.
It is consist of Day, Month, Year.
Java SE 8 isn't the same Java we know.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
ZoneDateTime
• It contain date and time along with time zone
details.
Java SE 8 isn't the same Java we know.
Year
• This object contain only Year information.
Java SE 8 isn't the same Java we know.
Which year is this ?
2016
YearMonth
• This object contains year and month
component only.
Java SE 8 isn't the same Java we know.
Credit Card expire date : 2019-11
Java SE 8 isn't the same Java we know.
MonthDay
• This object represent month and day
component only.
Java SE 8 isn't the same Java we know.
AMEX Card billing cycle : --02-22
Java SE 8 isn't the same Java we know.
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
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
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.
Get the time in Epoch seconds : 1453812691
Java SE 8 isn't the same Java we know.
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.
Output is in next slide
Java SE 8 isn't the same Java we know.
• 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.
toString() method return string in ISO
8601 format
Java SE 8 isn't the same Java we know.
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.
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.
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.
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
Date-Time Parsing
Java SE 8 isn't the same Java we know.
Output :-
Date parsing -> 2001-12-20
Time parsing -> 23:23
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.
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.
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.
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.
Custom Adjuster
Java SE 8 isn't the same Java we know.
Output :- 2016-02-28
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.
Java SE 8 isn't the same Java we know.
Output :- Is holiday ? -> true
Custom Query
TemporalQueries (Plural)
• This class provide predefine queries.
• E.g.  offset , precision query
Java SE 8 isn't the same Java we know.
Java SE 8 isn't the same Java we know.
Output
Period
• Period represent amount of time in terms of
years, months and days.
Java SE 8 isn't the same Java we know.
How many days passed after taken 2016 new year resolution ?
P16D
Java SE 8 isn't the same Java we know.
Duration
• Duration represent amount of time in terms of
seconds and nano-seconds.
Java SE 8 isn't the same Java we know.
Duration between two time : PT0.1S
Java SE 8 isn't the same Java we know.
Converting to a Non-ISO-Based Date
Java SE 8 isn't the same Java we know.
Output :-
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.
Look time piece makers .. How much you can make
Java SE 8 isn't the same Java we know.

Java 8 Date-Time API

  • 1.
    Date-Time (JSR-310) By – AnindyaBandopadhyay Java SE 8 isn't the same Java we know.
  • 2.
    Date Time (JSR-310) JavaSE 8 isn't the same Java we know.
  • 3.
    Why another setof 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.
    What is Java8 Date-Time offering ? (JSR-310) Java SE 8 isn't the same Java we know.
  • 5.
    Object are immutableas a result they are thread safe. Java SE 8 isn't the same Java we know.
  • 6.
    API’s are moreorganize and error free. Java SE 8 isn't the same Java we know.
  • 7.
    It allow differentcalendar system like Japanese, Thai Buddhist and other calendars. Java SE 8 isn't the same Java we know.
  • 8.
    Possible to writecustom query against new Date-Time Java SE 8 isn't the same Java we know.
  • 9.
    It provide accuracyat the range of nano second. Java SE 8 isn't the same Java we know.
  • 10.
    Basic understanding ofDate-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.
    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.
    What is Unixtime ? • 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.
    Problem because ofepoch time Java SE 8 isn't the same Java we know.
  • 14.
    Java SE 8isn't the same Java we know.
  • 15.
    Something interesting Source ofinformation 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.
    Overview of java.timepackage • 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.
    LocalDate Java SE 8isn't the same Java we know. This object only contain date component.
  • 18.
    It is consistof Day, Month, Year. Java SE 8 isn't the same Java we know.
  • 19.
    What is todaydate ? • 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.
    What is previousdate ? • 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.
    What is nextdate ? • 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.
    What is thelength 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.
    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.
    LocalTime • This objectonly 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.
    What is thetime ‘now’ ? • LocalTime timeNow = LocalTime.now(); • System.out.println("Time now : "+ timeNow); Java SE 8 isn't the same Java we know.
  • 26.
    Simply add anhour 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.
    Only take outminutes • 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.
    LocalDateTime • This objectcontain 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.
    LocalDate, LocalTime and LocalDateTimeobjects doesn’t contain information regarding time-zone. As it is ‘Local’. Java SE 8 isn't the same Java we know.
  • 30.
    Time Zone • Numerousof 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.
    Multiple group providerules • 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.
    Zone Id • AZoneId 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.
    ZoneOffset • This objectrepresent 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.
    ZoneDateTime • It containdate and time along with time zone details. Java SE 8 isn't the same Java we know.
  • 35.
    Year • This objectcontain only Year information. Java SE 8 isn't the same Java we know. Which year is this ? 2016
  • 36.
    YearMonth • This objectcontains year and month component only. Java SE 8 isn't the same Java we know.
  • 37.
    Credit Card expiredate : 2019-11 Java SE 8 isn't the same Java we know.
  • 38.
    MonthDay • This objectrepresent month and day component only. Java SE 8 isn't the same Java we know.
  • 39.
    AMEX Card billingcycle : --02-22 Java SE 8 isn't the same Java we know.
  • 40.
    OffsetTime • This objectrepresents time and offset from UTC/GMT. Java SE 8 isn't the same Java we know. 22:52:58.868+05:30
  • 41.
    OffsetDateTime • This objectrepresents 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.
    Instance • This objectrepresent 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.
    Get the timein Epoch seconds : 1453812691 Java SE 8 isn't the same Java we know.
  • 44.
    Clock • This objectreturn 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.
    Output is innext slide Java SE 8 isn't the same Java we know.
  • 46.
    • Get anInstant 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.
    toString() method returnstring in ISO 8601 format Java SE 8 isn't the same Java we know.
  • 48.
    Commonly Used Methods JavaSE 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.
    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.
    DateTimeFormatter • This classis 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.
    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.
    Date-Time Parsing Java SE8 isn't the same Java we know. Output :- Date parsing -> 2001-12-20 Time parsing -> 23:23
  • 53.
    Temporal • java.time.temporal packagesupport date, time code and in particular date time calculations. Java SE 8 isn't the same Java we know.
  • 54.
    Temporal Type • Applicationcode 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.
    TemporalAdjuster • This interfaceprovide 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.
    TemporalAdjusters (Plural) • Thisclass 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.
    Custom Adjuster Java SE8 isn't the same Java we know. Output :- 2016-02-28
  • 58.
    TemporalQuery<R> • This interfacecan 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.
    Java SE 8isn't the same Java we know. Output :- Is holiday ? -> true Custom Query
  • 60.
    TemporalQueries (Plural) • Thisclass provide predefine queries. • E.g.  offset , precision query Java SE 8 isn't the same Java we know.
  • 61.
    Java SE 8isn't the same Java we know. Output
  • 62.
    Period • Period representamount of time in terms of years, months and days. Java SE 8 isn't the same Java we know.
  • 63.
    How many dayspassed after taken 2016 new year resolution ? P16D Java SE 8 isn't the same Java we know.
  • 64.
    Duration • Duration representamount of time in terms of seconds and nano-seconds. Java SE 8 isn't the same Java we know.
  • 65.
    Duration between twotime : PT0.1S Java SE 8 isn't the same Java we know.
  • 66.
    Converting to aNon-ISO-Based Date Java SE 8 isn't the same Java we know. Output :-
  • 67.
    Interoperability with LegacyCode • 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.
    Look time piecemakers .. How much you can make Java SE 8 isn't the same Java we know.