+ All Categories
Home > Education > A Quick peek @ New Date & Time API of Java 8

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

Date post: 02-Dec-2014
Category:
Upload: buddha-jyothiprasad
View: 234 times
Download: 1 times
Share this document with a friend
Description:
A look at what is wrong with old java date and time API. New API is introduced in Java 8, this presentation of introduced this new API.
Popular Tags:
23
New Date & Time API of Java8 - Buddha Jyothiprasad
Transcript
Page 1: A Quick peek @ New Date & Time API of Java 8

New Date & Time API of Java8

- Buddha Jyothiprasad

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

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

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

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

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

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

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

Java User Group - Hyderabad

Fixed Code that doesn’t look right

Buddha

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

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….

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

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

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

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

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

Java User Group - HyderabadBuddha

Code for Creating Date & Time Objects

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

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

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

Java User Group - HyderabadBuddha

Date Manipulation examples

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

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

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

Java User Group - HyderabadBuddha

Time Zone examples

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

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

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

Java User Group - Hyderabad

Code snippets

Buddha

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

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

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

Java User Group - HyderabadBuddha

Parsing & Formatting snippets

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

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

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

Java User Group - Hyderabad

Working with Custom Queries

Buddha

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

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()

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

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

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

Java User Group - HyderabadBuddha


Recommended