Search code examples
javadate

How to get a Date object from year,month and day?


When I used the following code, the Date Object was wrong.

Date date = new Date(day.getYear(), day.getMonth(), day.getDay());

When I passed the values 2015, 8 and 5, I got

Sun Sep 05 00:00:00 CST 3915

Can anyone tell me how to get the Date Object From the value of year, month and day?

Day Class is defined by myself.


Solution

  • Without knowing more I'd have to guess but probably you didn't read the JavaDoc on that deprecated constructor:

    year the year minus 1900.
    month the month between 0-11.
    date the day of the month between 1-31.

    As you can see, if you want to create a date for today (Aug 5th 2015) you'd need to use new Date (115, 7, 5);

    If you see that documentation you are free to guess why this is deprecated and should not be used in any new code. :)