Java Reference
In-Depth Information
Introducing the Java 8 Date and Time API
Java 8 introduces a new package java.time , which contains the core classes that
most developers work with. It is split into four subpackages:
java.time.chrono
Alternative chronologies that developers using calendaring systems that do not
follow the ISO standard will interact with. An example would be a Japanese cal‐
endaring system.
java.time.format
Contains the DateTimeFormatter used for converting date and time objects
into a String and also for parsing strings into the data and time objects.
java.time.temporal
Contains the interfaces required by the core date and time classes and also
abstractions (such as queries and adjusters) for advanced operations with dates.
java.time.zone
Classes used for the underlying time zone rules; most developers won't require
this package.
One of the most important concepts when representing time is the idea of an
instantaneous point on the timeline of some entity. While this concept is well
defined within, for example, Special Relativity, representing this within a computer
requires us to make some assumptions. In Java 8, we represent a single point in time
as an Instant , which has these key assumptions:
• We cannot represent more seconds than can fit into a long .
• We cannot represent time more precisely than nanosecond precision.
This means that we are restricting ourselves to modeling time in a manner that is
consistent with the capabilities of current computer systems. However, there is
another fundamental concept that should also be introduced.
An Instant is about a single event in space-time. However, it is far from uncom‐
mon for programmers to have to deal with intervals between two events, and so Java
8 also introduces the java.time.Duration class. This class ignores calendar effects
that might arise (e.g., from daylight saving time). With this basic conception of
instants and durations between events, let's move on to unpack the possible ways of
thinking about an instant.
The parts of a timestamp
In Figure 9-1 , we show the breakdown of the different parts of a timestamp in a
number of possible ways.
m
n
s
a
 
Search WWH ::




Custom Search