From: Doug C. <de...@fl...> - 2002-02-19 07:32:23
|
I think I opened a can of worms. >>How can the jdbc driver extract the DATE portion of the 'milliseconds >>since January 1, 1970 00:00:00 GMT' accurately if it doesn't know the >>timezone? The jdbc driver could assume local time, or could assume >>GMT, but neither of these is as satisfactory as pulling the timezone >>from the original Date. > So, what is the _correct_ way to translate a java.util.Date to a > java.sql.Date. My apologies for ignorance of something I guess I really > should know.....! A comment in java.sql.Date says: > * 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. and the JDBC 2.1 spec says: > An application should create a Date object using a Calendar. The > application is responsible for specifying the time as 00:00:00 on > the desired date when using the Calendar since the JDBC API uses > this convention. In addition when creating a Time value the > application must specify a date of January 1, 1970 to the Calendar > used to create the millisecond value for the Time as this is the > convention specified for time. The comments in the jdbc 3.0 (jdk 1.4) java.sql.Date are clearer!?: > Constructs a Date object using the given milliseconds time value. If > the given milliseconds value contains time information, the driver > will set the time components to the time in the default time zone > (the time zone of the Java virtual machine running the application) > that corresponds to zero GMT. But this is ambiguous: how does the driver "know" if the "given milliseconds value contains time information?" If the user picks a timezone for the date, and sets the time to midnight there as described in the preceeding two comments, how can the driver distinguish this from any other random time? I guess the driver can set the minutes, seconds, and milliseconds to zero, (maybe?), but it can't touch the hours. Actually, there are some time zones that are not an integral number of hours from GMT! I think the jdbc date spec is crap. <soapbox> If I wrote the spec I'd say that *all* dates and *all* times are represented as if they were in GMT since dates and times don't really have timezones -- timestamps *do* have timezones, they should carry the GMT time and a separate timezone offset. </soapbox> Bottom line for Hibernate: 1. we could do nothing, and leave it to the user and the jdbc driver 2. we could get the timezone from the Date with getTimezoneOffset() and force the time portion of the Date to midnight in that timezone 3. we could use setDate(int parameterIndex, java.sql.Date x, Calendar cal) but not all drivers have it (plus, we'd have to construct the Claendar from the getTimezoneOffset() since jave.util.Date won't give it up)? I'd vote for 2 or 1 in that order, but I'd love to hear what others think. e |