Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get default Date and Time format pattern

Tags:

java

android

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):

  1. How to get default locale.

    Locale currentLocale= Locale.getDefault();
    System.out.print(currentLocale.toString());
    
    Result is "en_US"
    
  2. How to get decimal separator.

    DecimalFormatSymbols decimalFormatSymbols =
                                         new DecimalFormatSymbols(currentLocale);
    System.out.print(decimalFormatSymbols.getDecimalSeparator());
    
    Result is "."
    
  3. 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".
like image 201
Russian Bear Avatar asked May 12 '12 06:05

Russian Bear


1 Answers

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

SimpleDateFormatuses 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 ResourceBundleaccessed via the LocaleData.getDateFormatData()method.

Unfortunately the LocaleDataclass is a part of the internal sun.util.resources package.

like image 90
Johan Kaving Avatar answered Oct 21 '22 14:10

Johan Kaving