問題描述
最簡單的方法:
String[] namesOfDays = new String[7] {
"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"
};
此方法不使用語言環境.因此,如果系統語言不是英文,這種方法就不能正常工作.
This method does not use Locale. Therefore, if the system's language is not English, this method does not work properly.
使用 Joda 時間,我們可以這樣做:
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();
}
但是,此方法使用今天的日期和日歷計算,這對于最終目的是無用的.另外,它有點復雜.
However, this method uses today's date and calendar calculations, which are useless for the final purpose. Also, it is a little complicated.
有沒有一種簡單的方法來獲取像 "Sun"
, "Mon"
, ..., "Sat"
這樣的字符串和系統的默認語言環境?
Is there an easy way to get Strings like "Sun"
, "Mon"
, ..., "Sat"
with system's default locale?
推薦答案
如果我沒有誤會你
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()
這篇關于在 Java 中,如何使用系統的默認語言環境(語言)獲取星期幾(Sun、Mon、...、Sat)的字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!