mark
mark

Reputation: 2931

Java - GregorianCalendar outputting wrong year?

When I instantiate a new date using GregorianCalendar like this:

GregorianCalendar myCal = new GregorianCalendar(29,5,2011);

Then, finally, I carry out the following code (expecting it to return 2011), and it returns 35. I'm wondering why this is, as I will need to compare it to the passed date (2011).

System.out.println(myCal.get(Calendar.YEAR));

Upvotes: 0

Views: 486

Answers (2)

sgmorrison
sgmorrison

Reputation: 986

The GregorianCalendar constructor you are using does not expect dayOfMonth, month, year as arguments, but year, month, dayOfMonth. See the JavaDoc for details.

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340708

Are you even reading the documentation/method signatures?

public GregorianCalendar(int year, int month, int dayOfMonth)

Try this:

GregorianCalendar myCal = new GregorianCalendar(2011, Calendar.MAY, 29);

And BTW: 5th month is actually June, I guess you wanted May (months are 0-based). To avoid confusion use constants like Calendar.MAY (equal to 4...)

Upvotes: 5

Related Questions