問(wèn)題描述
最簡(jiǎn)單的方法:
String[] namesOfDays = new String[7] {
"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"
};
此方法不使用語(yǔ)言環(huán)境.因此,如果系統(tǒng)語(yǔ)言不是英文,這種方法就不能正常工作.
This method does not use Locale. Therefore, if the system's language is not English, this method does not work properly.
使用 Joda 時(shí)間,我們可以這樣做:
Using Joda time, we can do like this:
String[] namesOfDays = new String[7];
LocalDate now = new LocalDate();
for (int i=0; i<7; i++) {
/* DateTimeConstants.MONDAY = 1, TUESDAY = 2, ..., SUNDAY = 7 */
namesOfDays[i] = now.withDayOfWeek((DateTimeConstants.SUNDAY + i - 1) % 7 + 1)
.dayOfWeek().getAsShortText();
}
但是,此方法使用今天的日期和日歷計(jì)算,這對(duì)于最終目的是無(wú)用的.另外,它有點(diǎn)復(fù)雜.
However, this method uses today's date and calendar calculations, which are useless for the final purpose. Also, it is a little complicated.
有沒(méi)有一種簡(jiǎn)單的方法來(lái)獲取像 "Sun"
, "Mon"
, ..., "Sat"
這樣的字符串和系統(tǒng)的默認(rèn)語(yǔ)言環(huán)境?
Is there an easy way to get Strings like "Sun"
, "Mon"
, ..., "Sat"
with system's default locale?
推薦答案
如果我沒(méi)有誤會(huì)你
calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);
是您正在尋找的.這里 你可以找到文檔,
is what you are looking for. Here you can find the documentation,
或者您也可以使用 getShortWeekdays()
String[] namesOfDays = DateFormatSymbols.getInstance().getShortWeekdays()
這篇關(guān)于在 Java 中,如何使用系統(tǒng)的默認(rèn)語(yǔ)言環(huán)境(語(yǔ)言)獲取星期幾(Sun、Mon、...、Sat)的字符串的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!