Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > (Again) java.util.Date vs java.sql.Date

Reply
Thread Tools

(Again) java.util.Date vs java.sql.Date

 
 
grz01
Guest
Posts: n/a
 
      09-18-2009
Hi there,

(Again) Im trying to understand the EXACT difference between
java.util.Date vs java.sql.Date.
Googling, I can see that this is a very "popular" subject, but I still
cannot figure out it exactly.

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

So, apparently, java.sql.Date and java.util.Date have THE SAME
precision (at least down to the millisecs...).

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?

Most importantly: WHY does JDBC *need* to distinguish between them?

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
enforce this restriction, per the example above. So what's the REAL
point with this type, java.sql.Date?

Very greatful, if you can help me clarify this, once and for all.

TIA / grz01
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      09-18-2009
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'ŕtre.

> 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

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-18-2009
On Fri, 18 Sep 2009 06:30:03 -0700 (PDT), grz01 <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>Hi there,
>
>(Again) Im trying to understand the EXACT difference between
>java.util.Date vs java.sql.Date.


look at the source Most IDEs will let you do that with a few
keystrokes. You will see that java.sql.Date extends java.util.Date.

You will see code in java.sql.Date like this:

/**
* This method is deprecated and should not be used because SQL
Date
* values do not have a time component.
*
* @deprecated
* @exception java.lang.IllegalArgumentException if this method is
invoked
* @see #setSeconds
*/
public int getSeconds() {
throw new java.lang.IllegalArgumentException();
}

Sun is making sure you don't try to use some of the inherited Date
methods that don't apply to SQL dates.

Properly Sun should have used a pure date something like BigDate
instead of Date for SQL. Date is a UTC timestamp.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Perfect reusable components are not obtained at the first shot."
~ Bertrand Meyer (born: 1950 age: 59) 1989, creator of design by contract and the Eiffel language.
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      09-18-2009
grz01 wrote:
> (Again) Im trying to understand the EXACT difference between
> java.util.Date vs java.sql.Date.
> Googling, I can see that this is a very "popular" subject, but I still
> cannot figure out it exactly.
>
> 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
>
> So, apparently, java.sql.Date and java.util.Date have THE SAME
> precision (at least down to the millisecs...).


java.sql.Date extends java.util.Date and uses it for storage, so
obviously they store "the same information".

> 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" ?


DATE is a database type that only contains date part no time part.

When a Java Date contains zeroes for h, m, s and ms then it
matches perfectly with the database type DATE.

> How EXACTLY does it differ
> from a java.util.Date value?


Functionally: the toString method only shows y, m and d.

But programming wise it provides type safety.

> Most importantly: WHY does JDBC *need* to distinguish between them?


It forces the programmer to distinguish between the 3 different types:
date
date + time
time

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments