views:

185

answers:

1

JDBC offers me 3 different datatypes for time-related fields: "Date", "Time" and "Timestamp". Can someone provide a simple summary of what each one is used for and how to choose which to use for a given problem?

+8  A: 

Let's assume you have the date/time January 1, 2003 2:00pm stored in a database column. The three options are used as follows:

Use Date if you are only interested in the date portion of the date string.

  • ex: January 1, 2003

Use Time if you are only interested in the time portion of the date string

  • ex: 2:00pm

Use Timestamp if you want the date and time of the date string

  • ex: January 1, 2003 2:00pm
Mr. Will
Date stores the time also. Timestamp can have a finer resolution of time then Date.
Gandalf
This answer sounds good, but I am fairly sure that it is inaccurate. The java.sql.Date object contains a time (down to milliseconds).
mcherm
That is true, but when you retrieve a date using the ResultSet.getDate() method, it sets the time portion of the Date to 00:00. It's what JDBC calls "normalizing" when using getDate as getDate returns a java.util.Date object.
Mr. Will
Okay, after further research it turns out you are correct. Thank you.
mcherm