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

前端 未结 5 1281
孤街浪徒
孤街浪徒 2021-01-23 07:14

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

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

Can

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-23 07:34

    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. :)

提交回复
热议问题