What is the accepted way to replace java.util.Date(year,month,day)

前端 未结 3 1403
再見小時候
再見小時候 2020-12-15 16:25

I\'m trying to do something really simple, but starting to realize that dates in Java are a bit of minefield. All I want is to get passed groups of three ints ( a year, a m

3条回答
  •  抹茶落季
    2020-12-15 16:47

    The idea is to use the Calendar class, like so:

    Calendar cal = Calendar.getInstance();
    cal.set(year, month, date);
    Date date = cal.getTime();
    

    Indeed, if you check the Javadoc of the constructor you are mentioning, it is exactly what is suggested:

    Date(int year, int month, int date)
              Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).
    

    Or ... use JodaTime :-).

提交回复
热议问题