javadate

In java, Date() method returns a value. but I am confused in that


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?


Solution

  • 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)