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
2009-06-04 20:02:14
Date stores the time also. Timestamp can have a finer resolution of time then Date.
Gandalf
2009-06-04 20:55:00
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
2009-06-04 21:06:26
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
2009-06-04 21:13:27
Okay, after further research it turns out you are correct. Thank you.
mcherm
2009-06-05 15:28:50