As we have two date classes: java.sql.Date
and java.util.Date
. Whats the reason ResultSet#getDate()
returns the sql.Date
not util.Date
?
Agreed that sql.Date
extends util.Date
. But whats the reason of creating new class altogether when most of the methods in both the classes looks same?
Asked
Active
Viewed 537 times
0
-
1The documentation is pretty clear[ http://download.oracle.com/javase/6/docs/api/java/sql/Date.html ], and if you have a class for two different purposes, it's better that you'd have two classes. – MByD Jul 27 '11 at 17:56
-
3See [java.util.Date vs java.sql.Date](http://stackoverflow.com/questions/2305973/java-util-date-vs-java-sql-date) – Jonas Jul 27 '11 at 17:56
1 Answers
9
The reason is right in the JavaDocs of java.sql.Date
:
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
So a java.util.Date
has a "time" part, whereas in a java.sql.Date
the time is always "zero" because the (ANSI) SQL DATE
data type does not have a time either.
-
Same here! :) You'll also notice that the toString() and valueOf() methods are overridden to parse/set the ANSI date format. – Bill Brasky Jul 27 '11 at 17:59
-
I am extremely sorry . I did not get the meaning of this statement written in javadocs.I read it before posting the query.Coulkd you please elaborate it little more? – M Sach Jul 27 '11 at 18:31
-
I can only repeat myself: a DATE does not have a time, and that's what `java.sql.Date` ensures. – Jul 27 '11 at 19:13
-
So is there any way we can store time part also in data store.As java doc said sql date has no time part – M Sach Aug 07 '11 at 11:02