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
How can I get default Date and Time format pattern
提问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):
我已经发现的(可能对其他人有用):
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.
如何获取默认语言环境。
Locale currentLocale= Locale.getDefault(); System.out.print(currentLocale.toString()); Result is "en_US"
如何获得小数点分隔符。
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(currentLocale); System.out.print(decimalFormatSymbols.getDecimalSeparator()); Result is "."
如何获取默认的日期和时间格式模式(字符串);
(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 SimpleDateFormat
that returns the format string.
正如俄罗斯熊在他对问题的更新中提到的那样,有一种toLocalizedPattern()
方法SimpleDateFormat
可以返回格式字符串。
Original answer
原答案
SimpleDateFormat
uses 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 ResourceBundle
accessed via the LocaleData.getDateFormatData()
method.
所以实际的格式字符串似乎是ResourceBundle
通过LocaleData.getDateFormatData()
方法访问的。
Unfortunately the LocaleData
class is a part of the internal sun.util.resources
package.
不幸的是,LocaleData
该类是内部sun.util.resources
包的一部分。
回答by Emil H
I think what you want is to use the DateFormat
class. Check out the getDateInstance(int, Locale)
method.
我想你想要的是使用这个DateFormat
类。检查getDateInstance(int, Locale)
方法。