問(wèn)題描述
我需要獲取某個(gè)區(qū)域/國(guó)家/地區(qū)的一周的第一天.
I need to get the first day of week for a certain Locale / Country.
我嘗試使用以下代碼找到它:
I tried to find it using this code:
final Locale[] locales = new Locale[]{
new Locale("en_GB"),
new Locale("ru_RU"),
new Locale("en_US"),
new Locale("es_ES"),
new Locale("fr_FR"),
new Locale("iw_IL"),
new Locale("he_IL")
};
for (final Locale locale : locales) {
final Calendar cal = Calendar.getInstance(locale);
final int firstDayOfWeek = cal.getFirstDayOfWeek();
System.out.println(firstDayOfWeek);
}
這段代碼的結(jié)果是,所有這些語(yǔ)言環(huán)境都在檢索1",即星期日,而諸如 en_US
之類的語(yǔ)言環(huán)境應(yīng)該返回 2,即星期一.
The result of this code is that all of these locales are retrieving '1' which is Sunday, and locales such en_US
should return 2 which is Monday.
推薦答案
傳入兩個(gè)參數(shù) - language &國(guó)家/地區(qū),分開(kāi) - 而不是你的帶下劃線的字符串.請(qǐng)參閱 Oracle 教程.
Pass in two arguments –?language & country, separately – rather than your string with underscore. See Oracle Tutorial.
final Locale[] locales = new Locale[]{
new Locale("en", "GB"), // Pass language code, then country code, separately.
new Locale("ru", "RU"),
new Locale("en", "US"),
new Locale("es", "ES"),
new Locale("fr", "FR"),
new Locale("iw", "IL"),
new Locale("he", "IL")
};
然后我會(huì)使用 Java8 來(lái)獲取一周的第一天:
And then I would use Java8 to get the first day of the week:
DayOfWeek firstDayOfWeek = WeekFields.of(locale).getFirstDayOfWeek();
System.out.println(firstDayOfWeek);
輸出:
星期一
星期一
星期天
星期一
星期一
星期天
星期天
這篇關(guān)于如何獲得某個(gè)地區(qū)/國(guó)家/地區(qū)的第一天的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!