Could somebody tell me how can I get the default date and time format pattern(String) for specified locale(or default locale).
What I already found (Could be useful for somebody else):
How to get default locale.
Locale currentLocale= Locale.getDefault();
System.out.print(currentLocale.toString());
Result is "en_US"
How to get decimal separator.
DecimalFormatSymbols decimalFormatSymbols =
new DecimalFormatSymbols(currentLocale);
System.out.print(decimalFormatSymbols.getDecimalSeparator());
Result is "."
How to get the default Date and Time format pattern (String);
(do something)
System.out.print(something);
And got at output something like this: "dd/mm/yyyy" or "hh:mm:ss"..
or other values for specified locale.
Thanks a lot.
UPD: Found solution:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
String dateLocalizedFormatPattern = simpleDateFormat.toLocalizedPattern();
The result of System.out.print(dateLocalizedFormatPattern)
on my device is "M/d/yy h:mm a".
Update
As Russian Bear mentions in his update to the question there is a toLocalizedPattern()
method in SimpleDateFormat
that returns the format string.
Original answer
SimpleDateFormat
uses the following code in it's private constructor:
...
ResourceBundle r = LocaleData.getDateFormatData(loc);
if (!isGregorianCalendar()) {
try {
dateTimePatterns = r.getStringArray(getCalendarName() + ".DateTimePatterns");
} catch (MissingResourceException e) {
}
}
if (dateTimePatterns == null) {
dateTimePatterns = r.getStringArray("DateTimePatterns");
}
...
So the actual format strings seems to be in a ResourceBundle
accessed via the LocaleData.getDateFormatData()
method.
Unfortunately the LocaleData
class is a part of the internal sun.util.resources
package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With