I have a problem in java with sql date. I have the value of year-y, month-m,day-d I create new Date(y,m,d) but it says that this constructor is deprecated and it returns wrong date, how can I correctly set date, having y,m,d ?
6 Answers
According to the API,
As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).
You should use public Date(long date) where date is the number of milliseconds since January 1, 1970, 00:00:00 GMT
In your case you can also use:
Date mydate = Date.valueOf("2011-05-04"); // yyyy-mm-dd
1 Comment
Vahan
how can i get long Date, having year, month day ?
Calendar c = Calendar.getInstance();
c.set(year, month, date);
Date d = c.getTime();
1 Comment
Vahan
in this case cannot convert from java.util.date to sql.date
Use Calendar.
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, d);
c.set(Calendar.MONTH, m);
c.set(Calendar.YEAR, y);
Then if you have to use dates, then you can do the following -
Date d = c.getTime();
1 Comment
Vahan
in this case cannot convert from java.util.date to sql.date