Date post: | 02-Dec-2014 |
Category: |
Education |
Upload: | buddha-jyothiprasad |
View: | 234 times |
Download: | 1 times |
New Date & Time API of Java8
- Buddha Jyothiprasad
Java User Group - Hyderabad
Agenda
Buddha
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
Java User Group - Hyderabad3
A Quick Look at old API
java.util.Date java.sql.Date Calendar GregorianCalenda
r TimeZone DateFormat SimpleDateForma
t
Some bugs that doesn’t look like Bugs with old API
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 - Hyderabad6
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….
Java User Group - Hyderabad
Java 8 packages for Date & Time•Co
nsists 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
•Classes for supporting different timezones
java.time.zone
Buddha
Java User Group - Hyderabad8
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
Java User Group - HyderabadBuddha
Code for Creating Date & Time Objects
Java User Group - Hyderabad10
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
Java User Group - HyderabadBuddha
Date Manipulation examples
Java User Group - Hyderabad12
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, OffsetTime
Java User Group - HyderabadBuddha
Time Zone examples
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 - Hyderabad16
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.ofPattern() method
Java User Group - HyderabadBuddha
Parsing & Formatting snippets
Java User Group - HyderabadBuddha
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
Java User Group - Hyderabad
Working with Custom Queries
Buddha
Java User Group - Hyderabad20
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()
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 - HyderabadBuddha
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/format/DateTimeFormatter.html
http://www.journaldev.com/2800/java-8-date-time-api-example-tutorial-localdate-instant-localdatetime-parse-and-format
Buddha