使用java.util.Calendar的非标准区域设置 [英] non standard locale with java.util.Calendar

查看:267
本文介绍了使用java.util.Calendar的非标准区域设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在瑞典有一个客户,使用英语软件。所以我们设置 Locale(en,SV)。我们希望日历类能够遵守国家/地区设置,但它使用的语言和这个区域设置假设美国的设置。



所以我现在正在寻找一种让日历了解新 firstDayOfWeek minimumDayinFirstWeek 除了手动设置之外,标准方式的设置



澄清:2010年8月29日在瑞典CW 34(也在德国和英国),但在美国它被报告为CW 36.与01.01.2010是星期五和29.08.2010星期日的事实的不同结果。

我不能将语言设置本身更改为瑞典语,并且使用英语后备,因为我们不支持将瑞典语作为语言,而是Sun / Oracle / ..,因此Swing UI将有一个



只是添加一个名为sun.util.resources.CalendarData_en_SV.properties的属性文件不能正常工作。不会读!手动作为ResourceBundle是可能的。不知何故LocaleData.getCalendarData(Locale)在读取我不能找到的resourcfiles它自己的魔法,因为它的源不可用。这里调用方法: java.util.Calendar.setWeekCountData(Locale)



.util.spi包,但它不提供对第一个DayOfWeek和minimumDaysInFirstWeek设置的访问。



也许我可以尝试拦截对resourcebundles的调用,并使用默认回退英语,只有让CalendarData的调用进行!



现在我很无能为力...

p>

Ciao,



Nils Drews

  package de.drews.i18n; 

import java.util.Calendar;
import java.util.Locale;
import java.util.ResourceBundle;

public class Test {

/ **
* @param args
* /
public static void main(String [] args ){
// en_GB = 34
// en_US = 36
// sv_SV = 34
// en_SV = 36 - >错误

printTest(en,GB,34);
printTest(en,US,36);
printTest(sv,SV,34);
printTest(en,SV,34);
}

private static void printTest(String language,String country,int expected){
Locale locale = new Locale(language,country);

日历cal = Calendar.getInstance(locale);

cal.set(Calendar.YEAR,2010);
cal.set(Calendar.MONTH,Calendar.AUGUST);
cal.set(Calendar.DATE,29);

int actual = cal.get(Calendar.WEEK_OF_YEAR);

System.out.println(actual +\t+ expected +\t
+((actual == expected)?Yeah! )+\t+ language
+\t+ country);
}
}


解决方案

关于使用 getInstance(TimeZone zone,Locale aLocale)提供时区来选择日历行为和区域设置来定义语言?


we have a customer in sweden, using the software in english. So we set the Locale(en, SV). We did hope the Calendar class would adhere to the country settings, but it uses the language and with this locale it assumes US settings.

So i am now searching for a way to let the calendar get to know the new firstDayOfWeek and minimumDayinFirstWeek settings prefered by a standard way other than setting it manually and thus hardcoded.

For clarification: The 29. August 2010 is in sweden in CW 34 (also in Germany and Great Britain) but in the US it is reported as CW 36. The different results from the fact that the 01.01.2010 is a friday and the 29.08.2010 a sunday.

I cannot change the language setting itself to swedish and use the english fallback since we do not support swedish as language, but Sun/Oracle/.. does, so the Swing UI would have a mixture of swedish and english texts, which not acceptable.

And just adding a properties file named "sun.util.resources.CalendarData_en_SV.properties" does not work out: it does not get read! Manually as a ResourceBundle that's possible. Somehow LocaleData.getCalendarData(Locale) does its own magic in reading the resourcfiles which i cannot find out since the source of it is not available. The method is called here: java.util.Calendar.setWeekCountData(Locale).

I also found the java.util.spi package but it does not provide access to the firstDayOfWeek and minimumDaysInFirstWeek settings.

Perhaps i can try to intercept the calls to the resourcebundles and use the default fallback to english and only let the calls to CalendarData proceed!? But that sounds hacky.

For now i am clueless ...

Thx for any help!

Ciao,

Nils Drews

package de.drews.i18n;

import java.util.Calendar;
import java.util.Locale;
import java.util.ResourceBundle;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
       // en_GB = 34
       // en_US = 36
       // sv_SV = 34
       // en_SV = 36 --> wrong

       printTest("en", "GB", 34);
       printTest("en", "US", 36);
       printTest("sv", "SV", 34);
       printTest("en", "SV", 34);
    }

    private static void printTest(String language, String country, int expected) {
       Locale locale = new Locale(language, country);

       Calendar cal = Calendar.getInstance(locale);

       cal.set(Calendar.YEAR, 2010);
       cal.set(Calendar.MONTH, Calendar.AUGUST);
       cal.set(Calendar.DATE, 29);

       int actual = cal.get(Calendar.WEEK_OF_YEAR);

       System.out.println(actual + "\t" + expected + "\t"
        + ((actual == expected) ? "Yeah!" : "-") + "\t" + language
        + "\t" + country);
    }
}

解决方案

How about using getInstance(TimeZone zone, Locale aLocale) providing a timezone to select calendar behaviour and locale to define language?

这篇关于使用java.util.Calendar的非标准区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆