开发者

Android Localization: What am I doing wrong that I can get months be displayed according to the locale?

开发者 https://www.devze.com 2023-04-05 23:56 出处:网络
This is very simple, I would expect the following piece of code to display Gennaio (It开发者_开发技巧alian for January) but it still displays January. Even if I set the device locale to Italy. Any hel

This is very simple, I would expect the following piece of code to display Gennaio (It开发者_开发技巧alian for January) but it still displays January. Even if I set the device locale to Italy. Any help will be appreciated. Thanks,W.

    Calendar calendar= Calendar.getInstance(Locale.ITALIAN);
    calendar.set(2011,0,1);     
button.setText((calendar.getTime().toString()));


Your conclusion is logical but toString() is usually and certainly in this case a utility function, meant mainly for debugging.

For localised dates you need to use a DateFormatter, like so:

Calendar calendar=Calendar.getInstance();
 calendar.set(2011,0,1);
 String formatted=DateFormat.getLongDateFormat(this).format(calendar.getTime());
 button.setText(formatted);

This example uses one of the three standard date formatters but you can get quite specific about the format, particularly be calling DateFormat.getInstance(context).getDateInstance which allows you to set more parameters, including the locale. There's a litte more detail here: http://developer.android.com/reference/java/text/DateFormat.html

I hope that's what you were looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消