Creating java date object from year,month,day

JAVAGeek picture JAVAGeek · May 11, 2013 · Viewed 143.8k times · Source
int day = Integer.parseInt(request.getParameter("day"));  // 25
int month = Integer.parseInt(request.getParameter("month")); // 12
int year = Integer.parseInt(request.getParameter("year")); // 1988

System.out.println(year);

Calendar c = Calendar.getInstance();
c.set(year, month, day, 0, 0);  

b.setDob(c.getTime());

System.out.println(b.getDob());  

Output is:

1988
Wed Jan 25 00:00:08 IST 1989

I am passing 25 12 1988 but I get 25 Jan 1989. Why?

Answer

JB Nizet picture JB Nizet · May 11, 2013

Months are zero-based in Calendar. So 12 is interpreted as december + 1 month. Use

c.set(year, month - 1, day, 0, 0);