java sql date time

java.sql.Date represents a date, not a date and time. From the docs:

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be ‘normalized’ by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

If you want to store a date and time, you should look for another type – e.g. java.sql.Timestamp. EDIT: That’s not suggesting you use a TIMESTAMP column type – as paulsm4 says in the comments, that’s a different thing. However, as far as I can see, JDBC only supports:

  • Date (no, you want a time too)
  • Time (no, you want a date too)
  • Timestamp (includes a date and time, but you don’t want TIMESTAMP SQL semantics)

I would expect using the Java Timestamp type with a DATETIME column to work, although without the level of precision that Timestamp provides.

EDIT: After a bit more research, it looks like you may want to use the java.sql.Time type, but with special driver parameters – at least if you’re using the Microsoft driver. See these docs on configuring JDBC for more information.

Leave a Comment