Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using java.sql.Date Constructor with a long value

Tags:

java

date

sql

The deprecated java.sql.Date constructor was far more intuitive - Date(int year, int month, int day). But now we are required to use new java.sql.Date(long date).

I can't find on the documentation on how to write an actual long date myself. Is there any other approach to constructing an sql date in java besides using java.util.Date().getTime()?

like image 810
harryo Avatar asked Nov 03 '25 12:11

harryo


1 Answers

The reason that Date(year, month, day) is deprecated is because it's locale ignorant. If you want to just use the standard java classes, the standard way is to Calendar.getInstance, and then set the year, month and day using the .set methods of the resulting Calendar. However, as I've learned in 3 years of StackOverflow, the better way is to use Joda.

like image 118
Paul Tomblin Avatar answered Nov 06 '25 02:11

Paul Tomblin