New Date & Time API of Java8 
- Buddha Jyothiprasad
Agenda 
 A Quick look at Old API 
for Date and Time 
 Discuss the issues with 
it 
 Introduce the new API 
from Java 8 
 See some examples of 
various concepts in new 
API 
Buddha Java User Group - Hyderabad
A Quick Look at old API 
 java.util.Date 
 java.sql.Date 
 Calendar 
 GregorianCalendar 
 TimeZone 
 DateFormat 
 SimpleDateFormat 
Some bugs that doesn’t look like Bugs with old 
API 
3 Java User Group - Hyderabad
Issues with the old approach 
Ø Date is actually DateTime 
Ø Different class for SQL 
Ø Date doesn’t have timezone 
Ø getMonth() is zero-based, getYear() is 1900-based 
(i.e., the year 2009 is represented as 109) 
Ø Mutable hence not thread-safe by default 
Ø java.util.Date represents an instant on the timeline 
but invoking toString() prints time stamp along with 
time zone, causing confusion among developers 
Ø Calendar can’t format date directly 
Ø Arithmetic operations are still tricky 
Buddha Java User Group - Hyderabad
Fixed Code that doesn’t look right 
Buddha Java User Group - Hyderabad
New API 
 New package java.time 
 New classes LocalDate, 
LocalTime 
 A composite class 
LocalDateTime 
 Supports numerous 
ways of creating Date 
and Time objects 
 Supports Truncation, 
Timezones, Periods, 
Durations, Chronologies 
ANSI SQL Java SE 8 
DATE LocalDate 
TIME LocalTime 
TIMESTAMP LocalDateTime 
TIME WITH TIMEZONE OffsetTime 
TIMESTAMP WITH 
TIMEZONE 
OffsetDateTime 
And many more…. 
6 Java User Group - Hyderabad
Java 8 packages for Date & Time 
• Consists of major base classes 
• LocalDate, LocalTime, Instant, Duration 
java.time 
• Consists of generic API for non ISO Calendar 
Systems java.time.chrono 
• Contains temporal objects to find out specific 
date/time related like firstDay of month java.time.temporal 
• Classes for formatting and parsing date time 
objects java.time.format 
java.time.zone • Classes for supporting different timezones 
Buddha Java User Group - Hyderabad
Creating Objects 
 All the core classes are 
constructed by fluent 
factory methods 
 When constructing a 
value by its constituent 
fields, the factory is 
called of 
 when converting from 
another type, the factory 
is called from 
 There are also parse 
methods that take strings 
as parameters 
 Standard Java getter 
conventions are used in 
order to obtain values 
8 Java User Group - Hyderabad
Code for Creating Date & Time Objects 
9 Java User Group - Hyderabad
Date & Time manipulation 
 We can alter the object 
values in order to perform 
calculations 
 All objects are immutable 
 Setters are no more 
 New API also has a 
concept of Adjuster 
methods 
 These methods are called 
with and return new 
objects 
 There are also plus 
adjusters 
 We can write our own with 
adjusters and plus 
adjusters 
10 Java User Group - Hyderabad
Date Manipulation examples 
11 Java User Group - Hyderabad
Time Zones 
 The local classes that we 
looked at previously 
abstract away the 
complexity introduced by 
time zones 
 ZonedDateTime is a date 
and time with a fully 
qualified time zone 
 This can resolve an offset 
at any point in time 
 Other classes are ZoneId, 
ZoneOffset, 
OffsetDateTime, 
12OffsetTime Java User Group - Hyderabad
Time Zone examples 
13 Java User Group - Hyderabad
Instant, Periods & Duration 
 Instant is the closest sibling of java.util.Date. Instant 
class is used to work with machine readable time 
format, it stores date time in unix timestamp. 
 A Period represents a value such as “3 months and 
1 day,” 
 A Duration is a distance on the timeline measured in 
terms of time, and it fulfills a similar purpose 
to Period, but with different precision 
Buddha Java User Group - Hyderabad
Code snippets 
Buddha Java User Group - Hyderabad
Parsing & Formatting 
 Every class in date time 
formatter has methods for 
parsing and formatting 
 format() and parse() 
directly on the date 
objects 
 DateTimeFormatter 
 Loads of Predefined 
formatters like ISO_DATE 
 Custom formats can also 
be provided via 
DateTimeFormatter.ofPa 
ttern() method 
16 Java User Group - Hyderabad
Parsing & Formatting snippets 
17 Java User Group - Hyderabad
Query 
 A TemporalQuery can be used to retrieve 
information from a temporal-based object 
 The TemporalQueries class(note the plural) 
provides several predefined queries 
 The precision query, for example, returns the 
smallest ChronoUnit that can be returned by a 
particular temporal-based object 
 We can also create custom queries by implementing 
the interface TemporaryQuery 
18 Java User Group - Hyderabad
Working with Custom Queries 
Buddha Java User Group - Hyderabad
Legacy Support 
 Legacy Date/Time 
classes are used in 
almost all the 
applications 
 Backward compatibility 
is a must 
 Following Utility 
methods are provided 
for that purpose 
 toInstant() 
 toZoneId() 
 from() 
20 Java User Group - Hyderabad
Summary 
 Immutability: All the classes in the new API are immutable hence 
they are thread-safe. 
 Separation of Concerns: The new API separates clearly between 
human readable date time and machine time. It defines separate 
classes for Date, Time, DateTime, Timestamp etc. 
 Consistency: The methods are clearly defined and perform the 
same action in all the classes. There are format() and parse() 
methods defined in all these classes rather than having a separate 
class for them. 
 Utility operations: All the new Date Time API classes comes with 
methods to perform common tasks, such as plus, minus, format, 
parsing, getting separate part in date/time etc. 
 Extendable: The new Date Time API works on ISO-8601 calendar 
system but we can use it with other non ISO calendars as well. 
Buddha Java User Group - Hyderabad
Buddha Java User Group - Hyderabad
Sources 
 https://www.youtube.com/watch?v=OIg9lNpMJew 
 http://www.oracle.com/technetwork/articles/java/jf14- 
date-time-2125367.html 
 http://docs.oracle.com/javase/8/docs/api/java/time/fo 
rmat/DateTimeFormatter.html 
 http://www.journaldev.com/2800/java-8-date-time-api- 
example-tutorial-localdate-instant-localdatetime-parse- 
and-format 
Buddha Java User Group - Hyderabad

A Quick peek @ New Date & Time API of Java 8

  • 1.
    New Date &Time API of Java8 - Buddha Jyothiprasad
  • 2.
    Agenda  AQuick look at Old API for Date and Time  Discuss the issues with it  Introduce the new API from Java 8  See some examples of various concepts in new API Buddha Java User Group - Hyderabad
  • 3.
    A Quick Lookat old API  java.util.Date  java.sql.Date  Calendar  GregorianCalendar  TimeZone  DateFormat  SimpleDateFormat Some bugs that doesn’t look like Bugs with old API 3 Java User Group - Hyderabad
  • 4.
    Issues with theold approach Ø Date is actually DateTime Ø Different class for SQL Ø Date doesn’t have timezone Ø getMonth() is zero-based, getYear() is 1900-based (i.e., the year 2009 is represented as 109) Ø Mutable hence not thread-safe by default Ø java.util.Date represents an instant on the timeline but invoking toString() prints time stamp along with time zone, causing confusion among developers Ø Calendar can’t format date directly Ø Arithmetic operations are still tricky Buddha Java User Group - Hyderabad
  • 5.
    Fixed Code thatdoesn’t look right Buddha Java User Group - Hyderabad
  • 6.
    New API New package java.time  New classes LocalDate, LocalTime  A composite class LocalDateTime  Supports numerous ways of creating Date and Time objects  Supports Truncation, Timezones, Periods, Durations, Chronologies ANSI SQL Java SE 8 DATE LocalDate TIME LocalTime TIMESTAMP LocalDateTime TIME WITH TIMEZONE OffsetTime TIMESTAMP WITH TIMEZONE OffsetDateTime And many more…. 6 Java User Group - Hyderabad
  • 7.
    Java 8 packagesfor Date & Time • Consists of major base classes • LocalDate, LocalTime, Instant, Duration java.time • Consists of generic API for non ISO Calendar Systems java.time.chrono • Contains temporal objects to find out specific date/time related like firstDay of month java.time.temporal • Classes for formatting and parsing date time objects java.time.format java.time.zone • Classes for supporting different timezones Buddha Java User Group - Hyderabad
  • 8.
    Creating Objects All the core classes are constructed by fluent factory methods  When constructing a value by its constituent fields, the factory is called of  when converting from another type, the factory is called from  There are also parse methods that take strings as parameters  Standard Java getter conventions are used in order to obtain values 8 Java User Group - Hyderabad
  • 9.
    Code for CreatingDate & Time Objects 9 Java User Group - Hyderabad
  • 10.
    Date & Timemanipulation  We can alter the object values in order to perform calculations  All objects are immutable  Setters are no more  New API also has a concept of Adjuster methods  These methods are called with and return new objects  There are also plus adjusters  We can write our own with adjusters and plus adjusters 10 Java User Group - Hyderabad
  • 11.
    Date Manipulation examples 11 Java User Group - Hyderabad
  • 12.
    Time Zones The local classes that we looked at previously abstract away the complexity introduced by time zones  ZonedDateTime is a date and time with a fully qualified time zone  This can resolve an offset at any point in time  Other classes are ZoneId, ZoneOffset, OffsetDateTime, 12OffsetTime Java User Group - Hyderabad
  • 13.
    Time Zone examples 13 Java User Group - Hyderabad
  • 14.
    Instant, Periods &Duration  Instant is the closest sibling of java.util.Date. Instant class is used to work with machine readable time format, it stores date time in unix timestamp.  A Period represents a value such as “3 months and 1 day,”  A Duration is a distance on the timeline measured in terms of time, and it fulfills a similar purpose to Period, but with different precision Buddha Java User Group - Hyderabad
  • 15.
    Code snippets BuddhaJava User Group - Hyderabad
  • 16.
    Parsing & Formatting  Every class in date time formatter has methods for parsing and formatting  format() and parse() directly on the date objects  DateTimeFormatter  Loads of Predefined formatters like ISO_DATE  Custom formats can also be provided via DateTimeFormatter.ofPa ttern() method 16 Java User Group - Hyderabad
  • 17.
    Parsing & Formattingsnippets 17 Java User Group - Hyderabad
  • 18.
    Query  ATemporalQuery can be used to retrieve information from a temporal-based object  The TemporalQueries class(note the plural) provides several predefined queries  The precision query, for example, returns the smallest ChronoUnit that can be returned by a particular temporal-based object  We can also create custom queries by implementing the interface TemporaryQuery 18 Java User Group - Hyderabad
  • 19.
    Working with CustomQueries Buddha Java User Group - Hyderabad
  • 20.
    Legacy Support Legacy Date/Time classes are used in almost all the applications  Backward compatibility is a must  Following Utility methods are provided for that purpose  toInstant()  toZoneId()  from() 20 Java User Group - Hyderabad
  • 21.
    Summary  Immutability:All the classes in the new API are immutable hence they are thread-safe.  Separation of Concerns: The new API separates clearly between human readable date time and machine time. It defines separate classes for Date, Time, DateTime, Timestamp etc.  Consistency: The methods are clearly defined and perform the same action in all the classes. There are format() and parse() methods defined in all these classes rather than having a separate class for them.  Utility operations: All the new Date Time API classes comes with methods to perform common tasks, such as plus, minus, format, parsing, getting separate part in date/time etc.  Extendable: The new Date Time API works on ISO-8601 calendar system but we can use it with other non ISO calendars as well. Buddha Java User Group - Hyderabad
  • 22.
    Buddha Java UserGroup - Hyderabad
  • 23.
    Sources  https://www.youtube.com/watch?v=OIg9lNpMJew  http://www.oracle.com/technetwork/articles/java/jf14- date-time-2125367.html  http://docs.oracle.com/javase/8/docs/api/java/time/fo rmat/DateTimeFormatter.html  http://www.journaldev.com/2800/java-8-date-time-api- example-tutorial-localdate-instant-localdatetime-parse- and-format Buddha Java User Group - Hyderabad