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

前端 未结 5 1262
孤街浪徒
孤街浪徒 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:44

    • int getYear() Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.

    • int getMonth() Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH).

    • int getDay() Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_WEEK).

    Hence use :

    Date date = new Date(Calendar.get(Calendar.YEAR) - 1900, Calendar.get(Calendar.MONTH), Calendar.get(Calendar.DAY_OF_WEEK));

    Recomendation : Use Joda-Time

提交回复
热议问题