|
|
Locale-Sensitive Data |
TheFormatclass is an abstract base class that defines the API for formatting and parsing locale-sensitive data.Formatdeclares two important methods:formatwhichs formats locale-sensitive objects intoStrings, andparseObjectwhich parsesStrings back into objects. Anything that was formatted byformatis guaranteed to be parseable byparseObject.JDK 1.1 provides several subclasses of
Formatincluding intermediate abstract classes that define the programming interface for formatting and parsing specific types of data, and concrete classes that implement those APIs. Here's a class hierarchy diagram ofFormatand its descendents:
![]()
NumberFormatandDateFormatare both abstract subclasses ofFormat.NumberFormatdefines the interface for formatting and parsing numerical data such as 9.23458, .34, 54%, and 23.DateFormatdefines the interface for formatting and parsing date and time such as 25 May 96, 12:56 PM, and so on.The JDK provides concrete subclasses of both
NumberFormatandDateFormatbecause both are abstract and don't implement any formatting scheme themselves.The JDK implements two concrete subclasses of
NumberFormat:DecimalFormatthat formats decimal numbers, andChoiceFormatthat formats a number by choosing the resulting format from a range-specified list of options. These two concrete classes implement locale-sensitive formatting and parsing for a comprehensive set of locales. To find out the list of supportedLocales call thegetAvailableLocalesmethod.
SimpleDateFormatis the JDK's 1.1 concrete implementation of aDateFormat.SimpleDateFormatformats dates and times based on the Gregorian calendar. As with the number format classes,SimpleDateFormatsupports a comprehensive set of locales. To find out the list of supportedLocales call thegetAvailableLocalesmethod.The final descendent of
FormatisMessageFormat.MessageFormatis a concrete class and provides a means to produce concatenated messages in language-neutral way. Use this to construct messages displayed for end users.Getting a Formatter
The JDK formatters provide the locale-sensitive formatting for dates, times, numbers, and messages that is designed to be useful to most programmers, their data, and their formatting needs. Thus most programmers will use one of the formatters provided by the JDK (rather than writing their own).The most convenient way of getting a generally useful formatter is to call one of the factory methods provided by the abstract parent class for the data type you want to format. For example, to get a formatter for formatting a date, call one of the
getXxxxxInstancefactory methods provided byDateFormat. Similarly, to get a formatter for formatting a number, call one of thegetXxxxxInstancefactory methods provided byNumberFormat.So why not instantiate a formatter directly using a concrete class's constructor method? Well, the formatters are customizable and require a fair amount of initialization. Most programmers just want to format a date or number in the "normal" way and get on with writing the rest of their program. So, to make programmers' lives easier, the abstract format classes provide factory methods that you can use to create already-initialized formatters. The formatters provided by these factory methods format the data in a way that most programmers want.
The abstract format classes often provide several factory methods so that you can choose from among a group of already-initialized formatters. For example, to get a already-initialized formatter to format a date use
DateFormat.getDateInstance, to get a time formatter useDateFormat.getTimeInstance. Similarly, to get a formatter to format a money value useNumberFormat.getCurrencyInstanceand to get a formatter to format a percentage value useNumberFormat.getPercentInstance. Additionally, some factory methods take arguments that let you further control some formatter options. For instance, theDateFormatfactory methods lets you specify the time or date style from a short list. The formatters also let you set theLocalefor which the data should be formatted.If the factory methods don't provide a formatter that suits your needs, you can
- alter it by calling its setter methods,
- instantiate one directly and customize it yourself
- write your own format subclass
The
MessageFormatclass is an exception to this rule. Messages are all different so there is no notion of "the message that most programmers want". So to format messages, you either instantiate aMessageFormatusing its constructors or you use the class methodMessageFormat.format.Using a Formatter
Once you've got a formatter, you can use it to format your data. Call itsformatmethod and pass in an object to be formatted:ThemyFormatter.format(objectToFormat);formatmethod returns aStringthat contains the result of formatting the object. ThisStringcan be used as a GUI label, error message, or other item displayed to the user.You can also use a formatter to parse a
Stringthat was generated by the formatter:myFormatter.parse(formattedString);parsereturns anObjectof the appropriate type. TheObjectcontains the data value represented by theStringcontents. If the string was formatted by the formatter, it is guaranteed to be parseable.
|
|
Locale-Sensitive Data |