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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10561481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:34:13  来源:igfitidea点击:

How can I get default Date and Time format pattern

javaandroid

提问by Russian Bear

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.
    
  1. 如何获取默认语言环境。

    Locale currentLocale= Locale.getDefault();
    System.out.print(currentLocale.toString());
    
    Result is "en_US"
    
  2. 如何获得小数点分隔符。

    DecimalFormatSymbols decimalFormatSymbols =
                                         new DecimalFormatSymbols(currentLocale);
    System.out.print(decimalFormatSymbols.getDecimalSeparator());
    
    Result is "."
    
  3. 如何获取默认的日期和时间格式模式(字符串);

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

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".

采纳答案by Johan Kaving

Update

更新

As Russian Bear mentions in his update to the question there is a toLocalizedPattern()method in SimpleDateFormatthat returns the format string.

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

Original answer

原答案

SimpleDateFormatuses the following code in it's private constructor:

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

...
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.

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

Unfortunately the LocaleDataclass is a part of the internal sun.util.resourcespackage.

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

回答by Emil H

I think what you want is to use the DateFormatclass. Check out the getDateInstance(int, Locale)method.

我想你想要的是使用这个DateFormat类。检查getDateInstance(int, Locale)方法。