Localized Month Names SimpleDateFormat

I searched all over the site, but I think I have a slightly different problem, and I can really help with it before I have heart failure or burn my computer.

I am dynamically generating a list of month names (in the form June 2011, July 2011) and obviously I want this to be locale sensitive: so I use a simple date format object like this:

//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 the Polish word "September" there is the word "Veresyan" - pay attention to the emphasis in Russian. However, all I get is "Get in?"

What am I doing wrong?

Thanks to everyone - now I admit that this is a problem with the presentation, but how can I safely "read" the result from dtFormat - I added some comments below ref using getBytes etc. - this worked in other situations, I just can’t seem to get access to the result of the string without messing it up

- FINAL Editing; for everyone who comes on this

The answer was made on the BalusC blog: http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html#DevelopmentEnvironment

Basically the DTformat object returned UTF-8 and automatically converted back to the default system character set when I read it in a string

so this code worked for me

 new String(dtFormat.format(mCal.getTime()).getBytes("UTF-8"),"ISO-8859-1"); 

Thank you for help

+4
source share
2 answers

Your problem has nothing to do with SimpleDateFormat - you are doing the wrong thing with the result.

You did not tell us what you are doing with the line after - as you display it in the user interface, but this is a problem. You can see that it is retrieving a localized string; it is only a display of the accented character causing the problem. You would see exactly the same thing if you had a string constant containing the same accent character.

I suggest that you check all the encodings used throughout the application, if it is a web application, or check the font in which you display the string if it is a console application or a Swing application.

If you examine the string in the debugger, I'm sure that you will see that it has the correct characters - this is exactly how they get to the user, which is the problem.

+4
source

In my tests, dtFormat.format(mCal.getTime()) returns

październik 2011

new SimpleDateFormat(0,0,localeObject).format(mCal.getTime()) returns:

poniedziałek, 3 październik 2011 14:26:53 EDT

0
source

All Articles