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

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

    You can do it with a workaround if you're stuck to Java < 8, but it's very ugly:

    java.util.Date date = new java.text.SimpleDateFormat("dd.MM.yyyy").parse("05.08.2015");
    

    as @Thomas already stated, the default Constructor for date/month/year is deprecated. Probably take a look at this link if you have access to Java8.

提交回复
热议问题