java SimpleDateFormat 本地化月份名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7638988/
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
SimpleDateFormat localized month names
提问by Kelly
I have searched throughout the site but I think I have a slightly different issue and could really do with some help before I either have heart failure or burn the computer.
我在整个网站上进行了搜索,但我认为我有一个稍微不同的问题,在我出现心力衰竭或烧毁计算机之前,我真的可以得到一些帮助。
I dynamically generate a list of month names (in the form June 2011, July 2011) and obviously I want this to be locale sensitive: hence I use the simple date format object as follows:
我动态生成了一个月份名称列表(格式为 2011 年 6 月,2011 年 7 月),显然我希望它对区域设置敏感:因此我使用简单的日期格式对象,如下所示:
//the actual locale name is dependent on UI selection
Locale localeObject=new Locale("pl");
// intended to return full month name - in local language.
DateFormat dtFormat = new SimpleDateFormat("MMMM yyyy",localeObject);
//this bit just sets up a calendar (used for other bits but here to illustrate the issue
String systemTimeZoneName = "GMT";
TimeZone systemTimeZone=TimeZone.getTimeZone(systemTimeZoneName);
Calendar mCal = new GregorianCalendar(systemTimeZone); //"gmt" time
mCal.getTime(); //current date and time
but if I do this:
但如果我这样做:
String value=dtFormat.format(mCal.getTime());
this "should" return the localized version of the month name. In polish the word "September" is "Wrzesień" -- note the accent on the n. However all I get back is "Wrzesie?"
这“应该”返回月份名称的本地化版本。在波兰语中,“September”一词是“Wrzesień”——注意 n 上的重音。然而我得到的只是“Wrzesie?”
What am I doing wrong?
我究竟做错了什么?
Thanks to all - I accept now that it's a presentation issue - but how can I "read" the result from dtFormat safely - I added some comments below ref using getBytes etc. - this worked in other situations, I just can't seem to get access to the string result without messing it up
感谢所有人 - 我现在接受这是一个演示问题 - 但是我如何安全地“读取”来自 dtFormat 的结果 - 我使用 getBytes 等在 ref 下面添加了一些评论 - 这在其他情况下有效,我似乎无法访问字符串结果而不会弄乱它
-- FINAL Edit; for anyone that comes accross this issue
-- 最终编辑;对于遇到此问题的任何人
The answer was on BalusC's blog : http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html#DevelopmentEnvironment
答案在 BalusC 的博客上:http: //balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html#DevelopmentEnvironment
Basically the DTformat object was returning UTF-8 and was being automatically transformed back to the system default character set when I read it into a string
基本上,DTformat 对象返回 UTF-8,并在我将其读入字符串时自动转换回系统默认字符集
so this code worked for me
所以这段代码对我有用
new String(dtFormat.format(mCal.getTime()).getBytes("UTF-8"),"ISO-8859-1");
thank you very much for the assistance
非常感谢您的帮助
采纳答案by Jon Skeet
Your problem has nothing to do with SimpleDateFormat
- you're just doing the wrong thing with the result.
你的问题无关SimpleDateFormat
- 你只是对结果做错了。
You haven't told us what you're doing with the string afterwards - how you're displaying it in the UI - but that'sthe problem. You can see that it's fetching a localized string; it's only the display of the accented character which is causing a problem. You would see exactly the same thing if you had a string constant in there containing the same accented character.
你还没有告诉我们你之后对字符串做了什么——你如何在 UI 中显示它——但这就是问题所在。你可以看到它正在获取一个本地化的字符串;这只是导致问题的重音字符的显示。如果您有一个包含相同重音字符的字符串常量,您将看到完全相同的内容。
I suggest you check all the encodings used throughout your app if it's a web app, or check the font you're displaying the string in if it's a console or Swing app.
如果是 Web 应用程序,我建议您检查整个应用程序中使用的所有编码,或者如果它是控制台或 Swing 应用程序,请检查您显示字符串的字体。
If you examine the string in the debugger I'm sure you'll see it's got exactly the right characters - it's just how they're getting to the user which is the problem.
如果您在调试器中检查字符串,我相信您会看到它具有完全正确的字符——这正是它们如何到达用户那里的问题。
回答by Paul Hymanson
In my tests, dtFormat.format(mCal.getTime())
returns
在我的测试中,dtFormat.format(mCal.getTime())
返回
pa?dziernik 2011
pa?dziernik 2011
new SimpleDateFormat(0,0,localeObject).format(mCal.getTime())
returns:
new SimpleDateFormat(0,0,localeObject).format(mCal.getTime())
返回:
poniedzia?ek, 3 pa?dziernik 2011 14:26:53 EDT
poniedzia?ek, 3 pa?dziernik 2011 14:26:53 EDT