Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5063449

Doc: MessageFormat.setLocale() does not affect format() method

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 6
    • 5.0, 6
    • core-libs
    • None
    • beta
    • generic, sparc
    • generic, solaris_7

      According to the specification of MessageFormat.setLocale():
      Sets the locale to be used when creating or comparing subformats.This affects
      subsequent calls to the applyPattern and toPattern methods as well
      as to the format and formatToCharacterIterator methods.

      However, it seems that setLocale() does not affect output of
      format(Object[], StringBuffer, FieldPosition).

      See the following example:

      import java.util.*;
      import java.text.*;

      public class FormatTest2 {
          public static void main(String[] args) {
              Object date = new Date();
              Object[] arguments = {date};
              MessageFormat mf = new MessageFormat("{0,date}");
              mf.setLocale(Locale.UK);
              StringBuffer sb = new StringBuffer();
              sb = mf.format(arguments, sb, null);
              System.out.println("Formatted output from MessageFormat created using UK locale: " +
                      (new MessageFormat("{0,date}", Locale.UK)).format(arguments, new StringBuffer(), null));
              System.out.println("Formatted output from MessageFormat after locale set to UK: " + sb);
              String expected = (DateFormat.getDateInstance(DateFormat.DEFAULT, mf.getLocale())).format(date);
              System.out.println("Formatted output from expected SubFormat: " + expected);
          }
      }

      The output is:

      Formatted output from MessageFormat created using UK locale: 15-Jun-2004
      Formatted output from MessageFormat after locale set to UK: Jun 15, 2004
      Formatted output from expected SubFormat: 15-Jun-2004

            nlindenbsunw Norbert Lindenberg (Inactive)
            xwangsunw Xiaozhong Wang (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: