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
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
- duplicates
-
JDK-6452812 MessageFormat.setLocale() specification is incorrect
-
- Closed
-
- relates to
-
JDK-4058857 MessageFormat.setLocale doesn't change number or date formats
-
- Closed
-
-
JDK-4281978 MessageFormat's setLocale method does not work
-
- Closed
-