8
votes

I am trying to find out weekdays using DateFormatSymbols and here is a short programe

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 it is giving me folloiwng output

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

i am not sure why its giving total length as 8 while it should give it as 7

3

3 Answers

10
votes

The range of values for Calendar.{SUNDAY, MONDAY, ... SUNDAY } is 1-7. The docs for getShortWeekDays() state:

Returns: the short weekday strings. Use Calendar.SUNDAY, Calendar.MONDAY, etc. to index the result array.

So I'd expect an array which can be indexed with values 1-7... which means it has to have 8 elements (as all arrays in Java are 0-based).

1
votes

Days of the week in Java are 1-based, not 0-based. The writer 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;
}

in order to make it a bit simpler to perform day lookups.

1
votes
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;

        }