tags:

views:

96

answers:

2

I used Date() for getting the date of my birthday, but it was returned the mismatch of the month. My birthday is 04-March-87. so i gave an input as,

Date birthDay= new Date(87,03,04,8,30,00);

But it returns correct year, day and time. But month was problem. It displays April month.

What's wrong with that?

+5  A: 

The month in the Date class starts with 0 for January, so March is 2, not 3.

Also, the Date(int, int, int, int, int, int) constructor is deprecated, so you should consider using the Calendar class instead.

Finally, be careful with leading zeros in Java - they represent octal constants. The number 09 would not do what you expect.

Greg Hewgill
+1 for the warning about octals.
Grundlefleck
+7  A: 

Months are set from 0 to 11, January =0, February = 1, ..., December = 11.

So, for April do this:

Date birthDate = new Date(87,02,04,8,30,00); //March = 2

Hope this helps;

EDIT

The Date class with this constructor public Date(int year, int month, int date, int hrs, int min) is deprecated (i.e. it has been declared @Deprecated).

Rather do this.

Calendar calendar = Calendar.getInstance();
Calendar.set(1987, 2, 4, 0, 0, 0);
Date birthDate = calendar.getTime();

(It will return the same thing as what you asked for)

The Elite Gentleman