18.4 Core API for Formatting and Parsing of Values

The abstract class Format and its subclasses in the java.text package provide support for formatting and parsing of dates, times, numbers, currencies, and percentages (Figure 18.1).

A formatter has two primary functions. The first is to create a human-readable text representation of a value, called formatting. The second is to create a value from a character sequence, such as a string, containing a text representation of the value, called parsing.

In the rest of this chapter, we emphasize the locale-sensitive nature of formatting and parsing of different kinds of values.

  • Formatting numbers, currencies, and percentages (p. 1116)

The support for formatting numbers, currencies, and percentages is provided by the abstract class java.text.NumberFormat and its subclasses java.text.DecimalFormat and java.text.CompactNumberFormat.

  • Formatting dates and times (p. 1127)

The support provided for formatting dates and times by the abstract class java.text.DataFormat and its subclass java.text.SimpleFormat is aimed at objects of the java.util.Date legacy class, and is not covered in this book.

Support for the new Date and Time API is provided by the java.time package, which is covered extensively in Chapter 17, p. 1023. The class java.time.format.DateTimeFormatter provides the support for formatting date and time values created using the new Date and Time API.

The support for formatting messages containing numbers, currencies, dates, and times is provided by the concrete class java.text.MessageFormat. The class java.text.ChoiceFormat allows a choice of formatting dependent on numerical values, and is typically used for conditional formatting of messages.

The support provided by the MessageFormat class for date and time values is aimed at objects of the legacy class Date, but there are a number of ways that the MessageFormat class can be utilized with formatting objects of the new Date and Time API, and they are discussed later (p. 1139).

Leave a Reply

Your email address will not be published. Required fields are marked *