如何获取默认的日期和时间格式模式

Rus*_*ear 8 java android

有人可以告诉我如何获取指定区域设置(或默认区域设置)的默认日期和时间格式模式(String).

我已经找到的(可能对其他人有用):

  1. 如何获取默认语言环境.

    Locale currentLocale= Locale.getDefault();
    System.out.print(currentLocale.toString());
    
    Result is "en_US"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如何获得小数分隔符.

    DecimalFormatSymbols decimalFormatSymbols =
                                         new DecimalFormatSymbols(currentLocale);
    System.out.print(decimalFormatSymbols.getDecimalSeparator());
    
    Result is "."
    
    Run Code Online (Sandbox Code Playgroud)
  3. 如何获取默认的日期和时间格式模式(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.
    
    Run Code Online (Sandbox Code Playgroud)

非常感谢.

UPD: 找到解决方案:

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".
Run Code Online (Sandbox Code Playgroud)

Joh*_*ing 3

更新

正如 Russian Bear 在对问题的更新中提到的那样,有一种toLocalizedPattern()方法SimpleDateFormat可以返回格式字符串。

原答案

SimpleDateFormat在其私有构造函数中使用以下代码:

...
ResourceBundle r = LocaleData.getDateFormatData(loc);
if (!isGregorianCalendar()) {
    try {
        dateTimePatterns = r.getStringArray(getCalendarName() + ".DateTimePatterns");
    } catch (MissingResourceException e) {
    }
}
if (dateTimePatterns == null) {
    dateTimePatterns = r.getStringArray("DateTimePatterns");
}
...
Run Code Online (Sandbox Code Playgroud)

因此,实际的格式字符串似乎是ResourceBundle通过该LocaleData.getDateFormatData()方法访问的。

不幸的是,该类LocaleData是内部包的一部分sun.util.resources