harqs
harqs

Reputation: 179

Issue in setting date using calendar

I am trying to set the date as DEC 31,2012 using the following snippet ,but i am getting Mar 03,2013,what is wrong here.

Calendar today = Calendar.getInstance();
today.set(Calendar.MONTH, 13);
today.set(Calendar.DAY_OF_MONTH, 31);
today.set(Calendar.YEAR, 2012);

long calendarNeverEndDate = today.getTime().getTime();
System.out.println("calendarNeverEndDate:"
                   + sdf.formatLocal(calendarNeverEndDate));

Upvotes: 1

Views: 96

Answers (1)

Betlista
Betlista

Reputation: 10547

That's because months are 0-indexed (yes, it's inconsistent with days :-( ).

edit: As Romain mentioned, it's better to use constants

Calendar.DECEMBER

Upvotes: 5

Related Questions