javasqldate

Using java.sql.Date Constructor with a long value


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()?


Solution

  • 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.