raj
raj

Reputation: 1

Gettime is returning wrong date when called by Calendar object

I instantiate a GregorianCalendar and then set the time and dates.its showing perfectly okay when I display the Calendar Object but when I store it in Date variable by calling getTime() and display date variable, the date shown is somehow increased by 1 month. please help. the below code shows date as 2014-01-20

Calendar c=new GregorianCalendar();
    c.set(2013, 12, 20);
    Date d=c.getTime();
    System.out.println(d);

Upvotes: 0

Views: 373

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

Months are 0 based, so don't use numbers for months but rather the Calendar constants.

i.e.,

c.set(2013, Month.DECEMBER, 20);

Upvotes: 2

Related Questions