FiruzzZ
FiruzzZ

Reputation: 826

empty array position getting months

Is this a jdk/jre bug? I reported this when jdk7 was released but never received a feedback, is it normal get an array of 13 positions?

String[] months = DateFormatSymbols.getInstance().getMonths();
System.out.println(months.length + " " + Arrays.toString(months));

output: 13 [enero, febrero, marzo, abril, mayo, junio, julio, agosto, septiembre, octubre, noviembre, diciembre, ]

Upvotes: 3

Views: 902

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 339342

For January-December: java.time.Month

By the way, if you want just the 12-months of the ISO 8601 year (January-December), in modern Java use java.time.Month enum.

You can automatically localize the name of each month via the getDisplayName method.

Locale locale = Locale.of( "es" , "ES" ) ;  // Spanish language, Spain cultural norms.
SequencedCollection < String > monthNames =  
    Arrays
        .stream ( Month.values() )
        .map ( month -> month.getDisplayName ( TextStyle.FULL_STANDALONE , locale ) )
        .toList() ;
System.out.println( monthNames ) ;

See similar code run at Ideone.com.

[enero, febrero, marzo, abril, mayo, junio, julio, agosto, septiembre, octubre, noviembre, diciembre]

Upvotes: 0

adi
adi

Reputation: 1741

Not a bug. Looking at the javadoc of the source code ( java.text.DateFormatSymbols), it says:

Month strings. For example: "January", "February", etc. An array of 13 strings (some calendars have 13 months), indexed by Calendar.JANUARY, Calendar.FEBRUARY, etc.

String months[] = null;

Also, getWeekdays() method returns 8 values and so on.

public final static int JANUARY = 0;
public final static int FEBRUARY = 1;
public final static int MARCH = 2;
public final static int APRIL = 3;
public final static int MAY = 4;
public final static int JUNE = 5;
public final static int JULY = 6;
public final static int AUGUST = 7;
public final static int SEPTEMBER = 8;
public final static int OCTOBER = 9;
public final static int NOVEMBER = 10;
public final static int DECEMBER = 11;
public final static int UNDECIMBER = 12;

The API describes UNDECIMBER as: field indicating the thirteenth month of the year. Although GregorianCalendar does not use this value, lunar calendars do.

Read here for such calendars.

Upvotes: 8

TheGraduateGuy
TheGraduateGuy

Reputation: 1520

You can refer to this page:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/text/DateFormatSymbols.java

Some calendar have 13 months like lunar calendar,chinese leap year and that is why java have kept it like this.

Hope your doubt is clear.

Upvotes: 2

Related Questions