Pages

Friday, July 13, 2012

java.util.Date VS. java.sql.Date

from: http://stackoverflow.com/questions/2305973/java-util-date-vs-java-sql-date


accepted
Congratulations, you've hit my favorite pet peeve with JDBC: Date class handling.
Basically databases usually support at least three forms of datetime fields which are date, time and timestamp. Each of these have a corresponding class in JDBC and each of them extend java.util.Date. Quick semantics of each of these three are the following:
  • java.sql.Date corresponds to SQL DATE which means it stores years, months and days while hour, minute, second and millisecond are ignored. Additionally sql.Date isn't tied to timezones.
  • java.sql.Time corresponds to SQL TIME and as should be obvious, only contains information about hour, minutes, seconds and milliseconds.
  • java.sql.Timestamp corresponds to SQL TIMESTAMP which is exact date to the nanosecond (note that util.Date only supports milliseconds!) with customizable precision.
One of the commonest bugs in JDBC drivers in relation to these three types is that the types are handled incorrectly. This means that sql.Date is timezone specific, sql.Time contains current year, month and day et cetera et cetera.

Finally: Which one to use?

Depends on the SQL type of the field, really. PreparedStatement has setters for all three values, #setDate() being the one for sql.Date, #setTime() for sql.Time and #setTimestamp() for sql.Timestamp.
Do note that if you use ps.setObject(fieldIndex, utilDateObject); you can actually give a normal util.Date to most JDBC drivers which will happily devour it as if it was of the correct type but when you request the data afterwards, you may notice that you're actually missing stuff.

I'm really saying that none of the Dates should be used at all.

What I am saying that save the milliseconds/nanoseconds as plain longs and convert them to whatever objects you are using (obligatory joda-time plug). One hacky way which can be done is to store the date component as one long and time component as another, for example right now would be 20100221 and 154536123. These magic numbers can be used in SQL queries and will be portable from database to another and will let you avoid this part of JDBC/Java Date API:s entirely.

9 comments:

  1. Thanks for the blog. It was great to read.
    Java classes in Pune

    ReplyDelete
  2. Full stack Python developer course provides end-to-end web development training using Python. It explains frontend and backend technologies clearly. This full stack python developer course improves practical development skills. Students build complete dynamic web applications. Django framework and database connectivity are included. REST API development is covered. Deployment strategies are explained. It prepares job-ready full stack Python developers.

    ReplyDelete
  3. The service now admin course is perfect for beginners who want to learn how to handle day-to-day administrative tasks on the platform with practical exposure.service now admin course

    ReplyDelete
  4. Very informative post about MuleSoft training courses. It shows how they cover both beginner and advanced topics.mulesoft training courses

    ReplyDelete
  5. Mule soft certification can significantly boost career opportunities in integration and API management.mule soft certification

    ReplyDelete
  6. I found this post on ServiceNow training very useful. It clearly explains how learning this platform can boost career opportunities.servicenow training

    ReplyDelete
  7. Well explained! A structured data modeling class
    helps in mastering database design concepts. Hands-on learning ensures better understanding of normalization and data relationships.

    ReplyDelete
  8. cpq training is becoming essential today, and this article explains its importance very well.cpq training

    ReplyDelete