Java Reference
In-Depth Information
CHAPTER 4
Numbers and Dates
Numbers play a significant role in many applications. As such, it is helpful to know how
to use them correctly within the context of the work that you are trying to perform. This
chapter helps you understand how to perform some of the most basic operations with
numbers, and it also provides insight on performing advanced tasks such as working
with currency.
Dates are also important as they can be used for many purposes within an applica-
tion. In Java 8, the new Date-Time package called java.time was introduced. The
Date-Time API uses the calendar defined in ISO-8601 as the default. Therefore, the cal-
endar is based on the Gregorian calendar system, and in this chapter, you will learn how
to work with date, time, and time zone data. The Date-Time API adheres to several
design principles, in that it's clear, fluent, immutable, and extensible. The API uses a
clear language that is concise and very well defined. It is also very fluent, so code deal-
ing with date-time data is easy to read and understand. Most of the classes within the
Date-Time API are immutable, so in order to alter a date-time object you must create a
modified copy of the original. As such, many of the methods in the date-time classes are
named accordingly, such as of() and with() , so that you know you are creating a
copy rather than altering the original. Lastly, the new Date-Time API can be extended in
many cases, allowing it to be useful in many contexts.
The Date-Time API is made up of a rich set of classes, providing solutions that were
very difficult to achieve in previous APIs. Even though there are many different classes,
most of them contain a similar set of methods, so the same principles can be utilized
throughout all of the date and time units. Table 4-1 lists the common set of methods that
you will find in most of the date-time classes.
Table 4-1 . Date-Time API's Common Methods
 
 
Search WWH ::




Custom Search