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

Discussion in 'Java' started by grz01, Sep 18, 2009.

  1. grz01

    grz01 Guest

    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
     
    grz01, Sep 18, 2009
    #1
    1. Advertising

  2. grz01

    Lew Guest

    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
     
    Lew, Sep 18, 2009
    #2
    1. Advertising

  3. grz01

    Roedy Green Guest

    On Fri, 18 Sep 2009 06:30:03 -0700 (PDT), grz01 <>
    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.
     
    Roedy Green, Sep 18, 2009
    #3
  4. grz01

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Sep 18, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric
    Replies:
    3
    Views:
    2,941
    VisionSet
    Dec 2, 2003
  2. mgalaty
    Replies:
    0
    Views:
    2,728
    mgalaty
    Sep 10, 2004
  3. Replies:
    5
    Views:
    98,626
    vongeuca
    May 4, 2011
  4. BoBi
    Replies:
    3
    Views:
    4,192
  5. Replies:
    5
    Views:
    5,603
Loading...

Share This Page