在Java中,如何让周(周日,周一,...,周六)的天弦与系统的默认语言环境(语言)
问题描述:
最简单的方式:
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.
使用乔达时间,我们可以做这样的:
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.
有没有一种简单的方法来获得像周日,周一,弦乐...,周六与系统的默认语言环境?
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);
是你在找什么。 Here你可以找到的文件,
或者你也可以使用,getShortWeekdays()
String[] namesOfDays = DateFormatSymbols.getInstance().getShortWeekdays()