Java Reference
In-Depth Information
Bugs Bunny 22 2025551212
city = null
Daffy Duck 33 3035551212
city = null
By the way, the GregorianCalendar objects were also serialized, but the toString
method for GregorianCalendar prints out a lot of information, so I purposely left it out of
the toString method of Contact2 . (We discuss formatting dates in detail later in this chap-
ter.) If you open mycontacts.ser in a text editor, you will clearly see the two Gregorian-
Calendar objects serialized in the fi le.
Now that we have seen how to serialize and deserialize objects, let's turn our attention
to formatting and parsing data that uses the java.text package.
Formatting and Parsing Data
The java.text package contains classes and interfaces for handling text, dates, numbers,
and messages independent of the language that is being used in the application. According
to the exam objectives, you need to be able to “use standard J2SE APIs in the java.text
package to correctly format or parse dates, numbers, and currency values for a specifi c
locale.” This section discusses these topics in detail, including how to
Format and parse numbers and currency using the DecimalFormat and NumberFormat
classes in java.text .
Format and parse dates using the java.text.DateFormat class.
Format and Parse Numbers and Currency
The java.text.NumberFormat class is the abstract parent class of the number formatting
classes. The class contains static methods for getting appropriate formatter instances based
on the type of number you are formatting. For example, if you want to format currency,
use the static getCurrencyInstance method. Because formatting currency varies depending
on the language and culture of the users of your program, you can also specify the locale of
your specifi c users. Use a java.util.Locale object to represent your desired locale.
The following list gives the static methods in NumberFormat for obtaining instances of
NumberFormat :
public static final NumberFormat getInstance() is a general-purpose number
format that uses the default locale.
public static NumberFormat getInstance(Locale loc) is the same as the previous
method except that the format uses the specified locale.
public static final NumberFormat getNumberInstance() is intended for formatting
and parsing numbers in the default locale.
Search WWH ::




Custom Search