Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateFormatSymbols().getShortWeekdays() returning weekdays count as 8

Tags:

java

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

like image 637
Umesh Awasthi Avatar asked Jan 19 '12 10:01

Umesh Awasthi


1 Answers

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).

like image 109
Jon Skeet Avatar answered Sep 27 '22 18:09

Jon Skeet