Search code examples
javadate

Wrong (Date) date return in Java


I have the following line in my code:

this.date = new Date(year, month, day);

But when I give, for example:

year = 2008
month = 1
day = 20

I get:

Thu Feb 20 00:00:00 BRT 3908

Or let's say:

year = 2008
month = 3
day = 9

I get:

Thu Apr 09 00:00:00 BRT 3908

Any ideas what's wrong?


Solution

  • You should read the JavaDoc about the constructor. The parameters are not simply what you think they are.

    It says:

    year - the year minus 1900; must be 0 to 8099. (Note that 8099 is 9999 minus 1900.)
    month - 0 to 11
    day - 1 to 31

    However, as the Docs say as well, it is deprecated. Construct dates using a Calendar instead. Or use JodaTime.