Re: (Again) java.util.Date vs java.sql.Date
grz01 wrote:
(Again) Im trying to understand the EXACT difference between
java.util.Date vs java.sql.Date.
java.util.Date is for general representation of a datetime value.
java.sql.Date is specific to match the Java object model to the SQL
DATE type. It is speifically a mapping type.
Many writers claim that java.sql.Date only stores the DATE part (yyyy-
mm-dd) but not the TIME part (hh:MM:ss) of a Date/Time value, but that
I can easily disprove:
java.util.Date ud = new java.util.Date(=
);
java.sql.Date sd = new java.sql.Date(ud=
..getTime());
System.out.println(DateFormatUtils.format=
(ud, "yyyy-mm-dd
hh:MM:ss.SSS"));
System.out.println(DateFormatUtils.format=
(sd, "yyyy-mm-dd
hh:MM:ss.SSS"));
Output:
2009-17-18 03:09:36.635
2009-17-18 03:09:36.635
Of course, however the java.sql.Date is intended to represent SQL
data, not Java data, and the corresponding SQL type DATE does not
store time information, only date information.
So, apparently, java.sql.Date and java.util.Date have THE SAME
precision (at least down to the millisecs...).
True but not very relevant. What is relevant is the precision of the
SQL type, not the Java type.
And the official API documentation, really looks more confusing than
helpful to me::
"java.sql.Date:
A thin wrapper around a millisecond value that allows JDBC to identify
this as an SQL DATE value. A milliseconds value represents the
number of milliseconds that have passed since January 1, 1970
00:00:00.000 GMT.
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. "
Exactly what means "an SQL DATE value" ? How EXACTLY does it differ
from a java.util.Date value?
In order to use JDBC, you *must* have a fundamental understanding of
SQL. Part of that understanding is to know the data types the SQL
sjupports, including DATE. GIYF.
Most importantly: WHY does JDBC *need* to distinguish between them?
Because it is designed to work with SQL; that is its whole raison
d'=EAtre.
And, here again: "a java.sql.Date instance must be 'normalized' by
setting the hours, minutes, seconds, and milliseconds to zero in the
particular time zone..."
What does that mean exactly? Apparently, the constructor doesnt
It means exactly that you should set the hours, minutes, seconds and
milliseconds part of the time in the java.sql.Date instance to zero.
enforce this restriction, per the example above. So what's the REAL
point with this type, java.sql.Date?
The real point (no need to shout) is to match up to the SQL DATE type.
--
Lew