我正在尝试使用DateFormatSymbols查找工作日,这是一个简短的程序
String[] shortWeekdays = new DateFormatSymbols().getShortWeekdays();
System.out.println(shortWeekdays.length);
for (int i = 0; i < shortWeekdays.length; i++) {
String shortWeekday = shortWeekdays[i];
System.out.println("shortWeekday = " + shortWeekday);
}
它正在给我一个输出
- shortWeekday =
- shortWeekday = Sun
- shortWeekday = Mon
- shortWeekday = Tue
- shortWeekday = Wed
- shortWeekday = Thu
- shortWeekday = Fri
- shortWeekday =周六
我不确定为什么它的总长度为8,而它应该给它为7
答案 0 :(得分:10)
Calendar.{SUNDAY, MONDAY, ... SUNDAY }
的值范围是1-7。 getShortWeekDays()
州的文档:
返回:简短的工作日字符串。使用Calendar.SUNDAY,Calendar.MONDAY等来索引结果数组。
所以我期望一个可以用值1-7索引的数组......这意味着它必须有8个元素(因为Java中的所有数组都是基于0的)。
答案 1 :(得分:1)
Java中的星期几是基于1的,而不是基于0的。 DateFormatSymbols类的作者明确决定他会做以下的
private void initializeData(Locale desiredLocale) {
int i;
ResourceBundle resource = cacheLookup(desiredLocale);
// FIXME: cache only ResourceBundle. Hence every time, will do
// getObject(). This won't be necessary if the Resource itself
// is cached.
eras = (String[])resource.getObject("Eras");
months = resource.getStringArray("MonthNames");
shortMonths = resource.getStringArray("MonthAbbreviations");
String[] lWeekdays = resource.getStringArray("DayNames");
weekdays = new String[8];
weekdays[0] = ""; // 1-based
for (i=0; i<lWeekdays.length; i++)
weekdays[i+1] = lWeekdays[i];
String[] sWeekdays = resource.getStringArray("DayAbbreviations");
shortWeekdays = new String[8];
shortWeekdays[0] = ""; // 1-based
/*** start of what causes your odd behaviour **/
for (i=0; i<sWeekdays.length; i++)
shortWeekdays[i+1] = sWeekdays[i];
ampms = resource.getStringArray("AmPmMarkers");
localPatternChars = resource.getString("DateTimePatternChars");
locale = desiredLocale;
}
为了使执行日查找更简单。
答案 2 :(得分:0)
private String[] getWeekDayNames() {
String[] names = new DateFormatSymbols().getShortWeekdays();
List<String> daysName = new ArrayList<>(Arrays.asList(names));
daysName.remove(0);
//unComment bellow line if you want a start day Monday
//daysName.add(daysName.remove(0));
if (typedArray.getInt(noman.weekcalendar.R.styleable.WeekCalendar_dayNameLength, 0) == 0)
for (int i = 0; i < daysName.size(); i++)
//(0,3 MON) (0,1 M) (comment ".substring(0,3)" for full length of the day)
daysName.set(i, daysName.get(i).substring(0, 3));
names = new String[daysName.size()];
daysName.toArray(names);
return names;
}