Java Reference
In-Depth Information
Listing 12.1. Creating a LocalDate and reading its values
It's also possible to obtains the current date from the system clock using the now factory
method:
LocalDate today = LocalDate.now();
All the other date-time classes we'll investigate in the remaining part of this chapter provide a
similar factory method. You can also access the same information by passing a TemporalField to
the get method. The TemporalField is an interface defining how to access the value of a specific
field of a temporal object. The ChronoField enumeration implements this interface, so you can
conveniently use an element of that enumeration with the get method, as shown in the next
listing.
Listing 12.2. Reading LocalDate values using a TemporalField
int year = date.get(ChronoField.YEAR);
int month = date.get(ChronoField.MONTH_OF_YEAR);
int day = date.get(ChronoField.DAY_OF_MONTH);
Similarly, the time of day, such as 13:45:20, is represented by the LocalTime class. You can
create instances of LocalTime using two overloaded static factory methods named of. The first
one accepts an hour and a minute and the second one also accepts a second. Just like the
LocalDate class, the LocalTime class provides some getter methods to access its values, as
shown in the following listing.
Listing 12.3. Creating a LocalTime and reading its values
 
Search WWH ::




Custom Search