Java Reference
In-Depth Information
Table 6-5. New Date/Time API: Support
Description
Class
ZoneOffset A time offset from UTC (hours, minutes, seconds)
Defines a time zone such as Canada/Eastern , and its conversion rules
ZoneId
The new API is a “fluent” API, in which most operations return the object they have oper-
ated upon, so that you can chain multiple calls without the need for tedious and annoying
temporary variables:
LocalTime time = LocalTime.now().minusHours(5); // the time 5 hours ago
This results in a more natural and convenient coding style, in my opinion. You can always
write code with lots of temporary variables if you want; you're the one who will have to read
through it later.
Finding Today's Date
Problem
You want to find today's date and/or time.
Solution
Construct a LocalDate , LocalTime , or LocalDateTime object and call its toString( )
method.
Discussion
Each of these classes is abstract, so you will need to call one of its factory methods to get an
instance. They all provide a now() method, which does what its name implies. The Cur-
rentDateTime demo program shows simple use of all three:
Search WWH ::




Custom Search