Java Reference
In-Depth Information
Figure 9-1. Breaking apart a timestamp
The key concept here is that there are a number of different abstractions that might
be appropriate at different times. For example, there are applications where a
Local
Date
is key to business processing, where the needed granularity is a business day.
Alternatively, some applications require subsecond, or even millisecond precision.
Developers should be aware of their domain and use a suitable representation
within their application.
Example
The date and time API can be a lot to take in at first glance, so let's start by looking
at an example, and discuss a diary class that keeps track of birthdays. If you happen
to be very forgetful about birthdays, then a class like this (and especially methods
like
getBirthdaysInNextMonth()
) might be very helpful:
public
class
BirthdayDiary
{
private
Map
<
String
,
LocalDate
>
birthdays
;
public
BirthdayDiary
()
{
birthdays
=
new
HashMap
<>();
}
public
LocalDate
addBirthday
(
String
name
,
int
day
,
int
month
,
int
year
)
{
LocalDate
birthday
=
LocalDate
.
of
(
year
,
month
,
day
);
birthdays
.
put
(
name
,
birthday
);