SimpleDateFormat和基于区域设置的格式字符串
我试图根据给定的区域设置以不同的方式在Java格式化日期。例如,我希望英语用户能够看到2009年11月1日(格式为MMM d,yyyy)和挪威用户看到1. nov。2009(d。MMM。yyyy)。
I'm trying to format a date in Java in different ways based on the given locale. For instance I want English users to see "Nov 1, 2009" (formatted by "MMM d, yyyy") and Norwegian users to see "1. nov. 2009" ("d. MMM. yyyy").
如果我将区域设置添加到SimpleDateFormat构造函数中,月份部分可以正常工作,但是其余部分呢?
The month part works OK if I add the locale to the SimpleDateFormat constructor, but what about the rest?
我希望我可以添加格式字符串与语言环境配对SimpleDateFormat,但我找不到任何方法来做到这一点。是否可能或需要让我的代码检查区域设置并添加相应的格式字符串?
I was hoping I could add format strings paired with locales to SimpleDateFormat, but I can't find any way to do this. Is it possible or do I need to let my code check the locale and add the corresponding format string?
使用 DateFormat.getDateInstance( int style,Locale locale),而不是使用 SimpleDateFormat
创建自己的模式。
Use DateFormat.getDateInstance(int style, Locale locale) instead of creating your own patterns with SimpleDateFormat
.