如何获取基于语言环境的带时区的数据时间模式? [英] How to get the data time pattern with time zone based on the locale?
问题描述
以下代码是我已经拥有的代码:
The following code is what I already have:
DateFormat f = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Java_Locale);
SimpleDateFormat sf = (SimpleDateFormat) f;
String pattern = sf.toPattern();
使用上面的代码,我可以根据语言环境获得正确的日期/时间模式。例如:对于美国英语, M / d / yy h:mm a,对于中文, yy-Md ah:mm。
With the above code, I am able to get the PROPER date/time pattern based on the locale. For example: "M/d/yy h:mm a" for US English, "yy-M-d ah:mm" for Chinese.
但是,此模式不有时区信息。我希望能够将时区添加到模式中。例如,对于英语, M / d / yy h:mm a z,但我不想为其他语言环境指定模式。我希望根据给定的语言环境获得带有时区的正确模式,其他语言环境类似于 M / d / yy h:mm az。
However, this pattern does not have the time zone information. I hope to be able to add time zone into the pattern. For example, "M/d/yy h:mm a z" for English, but I don't want to specify the pattern for other locales. I hope to get the proper pattern with time zone based on a given locale, something similar to "M/d/yy h:mm a z" for other locales.
I使用Java 8。
I use Java 8.
推荐答案
DateFormat f = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, Java_Locale);
Full
时间样式包括时区。
The FULL
time style includes the time zone. This will make the time more complex, though.
注意:仅在模式字符串中添加 z
不会总是准确的。例如,阿拉伯语语言环境将时区置于日期和时间之间:
Note: just adding a z
to the pattern string is not always accurate. For instance, Arabic language locales put the time zone between the date and time:
Locale Java_Locale = Locale.forLanguageTag("ar");
DateFormat f = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, Java_Locale);
SimpleDateFormat sf = (SimpleDateFormat) f;
String pattern = sf.toPattern(); // "dd/MM/yy z hh:mm:ss a"
这篇关于如何获取基于语言环境的带时区的数据时间模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!