The class Format is an abstract base class that encapsulates locale-sensitive formatting and parsing. Three main subclasses are provided: NumberFormat, DateFormat and MessageFormat. These three also provide subclasses of their own.
Note - JDK 1.0 provided the Object.toString() method, which provided very simple formatting for all Java objects. This method remains in JDK 1.1. It does not, however, handle formatting in a locale-sensitive way. Its use should be limited to producing strings which are not user visible.
NumberFormat currencyFmtSimilar factory methods are provided for obtaining a locale's general purpose number format and its percent number format.
= NumberFormat.getDefaultCurrency( Locale.GERMANY );
System.out.println("My salary: " + currencyFmt( 24.63 ));
The class DecimalFormat is a concrete subclass of NumberFormat that can format decimal numbers such as are used in Western, Arabic or Indic languages. Programmers generally would not instantiate this class directly but would use the factory methods described above.
DecimalFormat has the ability to take a pattern string to specify how a number should be formatted. The pattern specifies things like the precision of the number, whether leading zeros should be printed, what currency symbols are used, etc. The pattern string can be altered if a program needs to create a custom format.
DateFormat uses a Calendar and TimeZone object in order to interpret time values. By default, a DateFormat for a given locale will use the appropriate Calendar for that locale and the system's default TimeZone. The programmer can override these choices if desired.
The class SimpleDateFormat is a concrete subclass of DateFormat. SimpleDateFormat includes the ability to customize the time format using a pattern string. Here is an example that customizes the US locale's time format to look like "08:15 am."
DateFormat fmtA complete description of pattern strings can be found in the JavaDoc for SimpleDateFormat.
= DateFormat.getDateFormat( DateFormat.DEFAULT, Locale.USA );
fmt.setPattern("hh:mm a",false);
System.out.println( fmt.format(System.currentTimeMillis()) );
int numFiles = 3;The above code, while easy to understand, is extremely hard to localize because it hard-codes both the strings that make up the message and the order in which they are put together. Note, for example, that a French translation of the message, "Il y a 3 fichiers sur le disque 'MyDisk'.", reverses the parameters.
String diskName = "MyDisk";
String message = "The disk `" + diskName
+ "` contains " + numFiles + " files.";
The class MessageFormat provides a way to build messages in a language-neutral way. It is constructed from a pattern string. The pattern string describes the structure of the message and the substitution order for the parameters. Using a MessageFormat, the above example would look like this:
Object[] arguments = new Object[2];The Format method formats the given arguments then substitutes the result into the pattern string to form the final message. There are several ways the MessageFormat tries to format the given arguments. An array of Format objects can be passed to the MessageFormat. If this is present, parameter n will be formatted using the nth entry of the format array. If an explicit Format has not been given for a parameter, then a default Format will be obtained. If the parameter to be formatted is a Number then NumberFormat.getDefault() is called. Otherwise, the parameter's toString() method is called.
arguments[0] = 3;
arguments[1] = "MyDisk";
StringBuffer message = new StringBuffer();
MessageFormat fmt
= new MessageFormat("The disk `%0' contains %1 files.");
fmt.format(arguments, message, null);
fmt.setPattern("Il y a %1 fichiers sur le disque `%0'");
fmt.format(arguments, message, null);
An additional type of Format called a ChoiceFormat is available for use in formatting the parameters of a message. A ChoiceFormat allows text to be associated with a number or range of numbers. See the JavaDoc description of java.util.ChoiceFormat for details.