This formatter will format a java.util.Date object (which represents both date and time) by extracting its time components and formatting them according to the format defined by DateFormat.SHORT and taking the locale into consideration. If a MessageFormat instance does not specify the locale, then the default locale is used.

Format elements that specify the format types date and time are compatible with the java.util.Date legacy class, with subsequent reliance on the DateFormat class to provide an appropriate formatter. This presents a slight problem when we want to format date and time values of the new Date and Time API. We will use the static methods of the utility class ConvertToLegacyDate (§17.8, p. 1088) to convert Local-Date, LocalTime, LocalDateTime, and ZonedDateTime objects to Date objects in order to leverage the format element handling functionality of the MessageFormat class.

The pattern that we saw earlier with just vanilla format elements (i.e., format elements with just the argument index) is now shown at (1) below to include the specification of the type and style of the format elements. A MessageFormat instance that is based on the pattern at (1) is created at (2) using a constructor of the Message-Format class. The arguments that we want to format are included in the Object array at (3). Note that the element index in this array corresponds to an argument index in the format elements specified in the pattern. We use the methods from the utility class ConvertToDate to convert LocalTime and LocalDate values to Date values (§17.8, p. 1088). Finally, at (4), the argument array is passed for formatting to the format() method inherited by the MessageFormat class from its superclass Format. The code below also outlines the pertinent steps that are involved when using a Message-Format instance to format compound messages.

Click here to view code image

// Specify the pattern:                                                       (1)
String pattern2 = “At {3,time,short} on {2,date,medium} Elvis landed at {0} “
               +  “and was greeted by {1,number,integer} fans.”;
// Create a MessageFormat based on the given pattern:                         (2)
MessageFormat mf2 = new MessageFormat(pattern2);
// Create the array with the arguments to format:                             (3)
Object[] messageArguments = {
    “Honolulu”,                                         // argument index 0
    3000,                                               // argument index 1
    ConvertToDate.ldToDate(LocalDate.of(1961,3,25)),    // argument index 2
    ConvertToDate.ltToDate(LocalTime.of(12,15))         // argument index 3
};
// Format the arguments:                                                      (4)
String output2 = mf2.format(messageArguments);
System.out.println(output2);

Output from the code:

Click here to view code image

At 12:15 PM on Mar 25, 1961 Elvis landed at Honolulu and was greeted by 3,000 fans.

This output is different from the output we saw earlier when using vanilla format elements. In this case, appropriate formatters are implicitly created for each format element that apply the format style and take into consideration the locale (in this case, it is the default locale, which happens to be the US locale).

The following constructors defined by the MessageFormat class are used in the examples in this section:

Click here to view code image

MessageFormat(String pattern)
MessageFormat(String pattern, Locale locale)

Create a message formatter to apply the specified pattern. The created formatters will format or parse according to the default locale or the specified locale, respectively.

Selected methods are provided by the MessageFormat class, many of which are used in the examples in this section:

Click here to view code image

final String format(Object obj)       // Inherited from the Format class.

Returns a string that is the result of formatting the specified object. Passing an Object[] formats the elements of the array individually.

Click here to view code image

static String format(String pattern, Object… arguments)

This static method implicitly creates a one-time formatter with the given pattern, and returns the result of using it to format the variable arity arguments.

void setLocale(Locale locale)

Sets the locale to be used when creating subformats. Subformats already created are not affected.

Locale getLocale()

Returns the locale used by this formatter.

void applyPattern(String pattern)

Sets the pattern used by this formatter by parsing the pattern and creating the necessary subformats.

String toPattern()

Returns a pattern that represents the current state of this formatter.

Click here to view code image

void setFormat(int formatElementIndex, Format newFormat)
void setFormats(Format[] newFormats)

The first method sets the format to use for the format element indicated by the formatElementIndex in the previously set pattern string.

The second method sets the formats to use for the format elements in the previously set pattern string. Note that the order of formats in the newFormats array corresponds to the order of format elements in the pattern string. Contrast this method with the setFormatsByArgumentIndex() method.

More formats provided than needed by the pattern string are ignored. Fewer formats provided than needed by the pattern string results in only the provided formats being inserted.

Click here to view code image

void setFormatByArgumentIndex(int argumentIndex, Format newFormat)
void setFormatsByArgumentIndex(Format[] newFormats)

The first method sets the format to use for the format element indicated by the argumentIndex in the previously set pattern string.

The second method sets the formats to use for the format elements in the previously set pattern string. However, note that the order of formats in the new-Formats array corresponds to the order of elements in the arguments array passed to the format methods or returned by the parse methods. Contrast this method with the setFormats() method.

Any argument index not used for any format element in the pattern string results in the corresponding new format being ignored.

Fewer formats being provided than needed results in only the formats for provided argument indices being replaced.

Leave a Reply

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