views:

885

answers:

2

How can I find the DateFormat for a given Locale?

+4  A: 
DateFormat.getDateInstance(int,Locale)

For example:

import static java.text.DateFormat.*;

DateFormat f = getDateInstance(SHORT, Locale.ENGLISH);

Then you can use this object to format Dates:

String d = f.format(new Date());

If you actually want to know the underlying pattern (e.g. yyyy-MMM-dd) then, as you'll get a SimpleDateFormat object back:

SimpleDateFormat sf = (SimpleDateFormat) f;
String p1 = sf.toPattern();
String p2 = sf.toLocalizedPattern();
oxbow_lakes
I don't think there is a guarantee that DateFormat.getDateInstance(...) returns a SimpleDateFormat object, so casting the result to SimpleDateFormat might be dangerous (it might not work on another implementation of Java than Sun's implementation, or it might not even work on a different version of Sun's implementation).
Jesper
This is very true - there is no guarantee. But it will be a `SimpleDateFormat` in practice
oxbow_lakes
A: 

Is there any other way (without using SimpleDateFormat cast) to retrieve default pattern for a given Locale object?

Related theme.

Milkywayfarer