DateFormatSymbols (). GetShortWeekdays () Returns weekdays as 8

I am trying to find the days of the week using DateFormatSymbols and here is a short program

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); } 

and he gives me the following result

  • shortWeekday =
  • shortWeekday = Sun
  • shortWeekday = Mon
  • shortWeekday = Tue
  • shortWeekday = Wed
  • shortWeekday = Thu
  • shortWeekday = Fri
  • shortWeekday = Sat

I'm not sure why he gives a total length of 8, while she should give her 7

+7
source share
2 answers

The value range for Calendar.{SUNDAY, MONDAY, ... SUNDAY } is 1-7. Docs for getShortWeekDays() state:

Returns: short lines of the day of the week. Use Calendar.SUNDAY, Calendar.MONDAY etc. to index the array of results.

So, I would expect an array that can be indexed with values โ€‹โ€‹of 1-7 ... which means that it should have 8 elements (since all arrays in Java are based on 0).

+10
source

The days of the week in Java are based on 1 rather than 0. The author of the DateFormatSymbols class clearly decided that he would do the following

 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; } 

to make it easier for daily searches.

+1
source

All Articles