How can I find the DateFormat
for a given Locale
?
views:
885answers:
2
+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 Date
s:
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
2009-08-27 07:13:21
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
2009-08-27 07:30:45
This is very true - there is no guarantee. But it will be a `SimpleDateFormat` in practice
oxbow_lakes
2009-08-27 07:31:56
A:
Is there any other way (without using SimpleDateFormat cast) to retrieve default pattern for a given Locale object?
Milkywayfarer
2010-08-15 18:16:15