Convert java.util.Date to java.sql.Date

Discussion in 'Java' started by Bumsys@gmail.com, Jan 22, 2008.

  1. Guest

    I convert java.util.Date to java.sql.Date this way:

    java.util.Date utilDate = new java.util.Date();
    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

    And then send sqlDate to prepareStatement.
    ps.setDate(1, sqlDate).
    But I got the date such as dd-mm-yyyy. And I need also take hours,
    minutes pm(am).

    How can I get the Date "dd-mm-yyyy hh:mm a"?
    , Jan 22, 2008
    #1
    1. Advertising

  2. Lew Guest

    wrote:
    > I convert java.util.Date to java.sql.Date this way:
    >
    > java.util.Date utilDate = new java.util.Date();
    > java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
    >
    > And then send sqlDate to prepareStatement.
    > ps.setDate(1, sqlDate).
    > But I got the date such as dd-mm-yyyy. And I need also take hours,
    > minutes pm(am).
    >
    > How can I get the Date "dd-mm-yyyy hh:mm a"?


    The Java Date holds its data as milliseconds since epoch - you have the hours,
    minutes and seconds down to the millisecond in there already.

    If you are asking how to *display* the Date down to the second, then consider
    using things like
    <http://java.sun.com/javase/6/docs/api/java/text/DateFormat.html>
    or
    <http://java.sun.com/javase/6/docs/api/java/util/Calendar.html>

    If you're asking how to get the database to *store* the date to a certain
    resolution, that depends on how you define the column in which you store the
    data, which in turn depends on the SQL dialect your DBMS uses. Check the docs
    for your DBMS, and if necessary redefine the column to hold the correct
    resolution of your data.

    The SQL standard defines TIMESTAMP WITH TIME ZONE, which is what you need.
    Different DBMSes support it differently. (MySQL is completely non-compliant.
    Among other things, it doesn't store time zones.)
    <http://troels.arvin.dk/db/rdbms/#data_types-date_and_time>

    --
    Lew
    Lew, Jan 22, 2008
    #2
    1. Advertising

  3. GArlington Guest

    On Jan 22, 2:54 pm, wrote:
    > I convert java.util.Date to java.sql.Date this way:
    >
    > java.util.Date utilDate = new java.util.Date();
    > java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
    >
    > And then send sqlDate to prepareStatement.
    > ps.setDate(1, sqlDate).
    > But I got the date such as dd-mm-yyyy. And I need also take hours,
    > minutes pm(am).
    >
    > How can I get the Date "dd-mm-yyyy hh:mm a"?


    1) Make sure that your data fields are defined as DateTimeStamp (or is
    it just TimeStamp?)
    2) Try to use java.sql.Timestamp
    GArlington, Jan 22, 2008
    #3
  4. Guest

    when I convert java.util.Date to java.sql.Date I want to get sqlDate
    in formate "dd-mm-yyyy hh:mm a"???
    , Jan 22, 2008
    #4
  5. Lew Guest

    wrote:
    > when I convert java.util.Date to java.sql.Date I want to get sqlDate
    > in formate "dd-mm-yyyy hh:mm a"???


    java.sql.Date does not *have* "formats". As I mentioned, it only stores
    milliseconds since epoch. That means it already contains the time down to the
    millisecond.

    Review my recommendation about java.text.DateFormat. Did you consider the
    information I provided upthread?

    GArlington mentioned java.sql.Timestamp. Aside from its more natural match to
    the SQL TIMESTAMP type, Timestamp holds time to nanosecond resolution. Just
    like java.util.Date and its other offspring, java.sql.Date, Timestamp also
    does not have a "format" regarding "hh:mm", etc.

    Sidebar: The Javadocs refer to the "precision of a Timestamp object" in terms
    of the number of characters in its String representation. That is bogus.
    Like its parent, java.util.Date, Timestamp holds long values that represent
    milliseconds (actually seconds) and nanoseconds since epoch. Its precision
    therefore is nanoseconds. I don't know what they were thinking when they
    wrote that part about "precision" being "19" - not even any units specified.

    Much as I am in favor of the Javadocs, occasionally they disappoint.

    Anyway, OP, for your purposes remember that java.util.Date and its offspring
    DO NOT HAVE FORMATS for the date. The only "format" they have is one or two
    long values.

    Please review the advice given earlier.

    --
    Lew
    Lew, Jan 22, 2008
    #5
  6. Lew Guest

    wrote:
    >> I convert java.util.Date to java.sql.Date this way:
    >>
    >> java.util.Date utilDate = new java.util.Date();
    >> java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
    >>
    >> And then send sqlDate to prepareStatement.
    >> ps.setDate(1, sqlDate).
    >> But I got the date such as dd-mm-yyyy. And I need also take hours,
    >> minutes pm(am).
    >>
    >> How can I get the Date "dd-mm-yyyy hh:mm a"?


    GArlington wrote:
    > 1) Make sure that your data fields are defined as DateTimeStamp (or is
    > it just TimeStamp?)


    The SQL standard specifies TIMESTAMP. Some DBMSes deviate from the standard.
    (MySQL in significant ways. Its "TIMESTAMP" is not even remotely like the
    SQL standard type.)

    > 2) Try to use java.sql.Timestamp


    This is good advice,. According to the Javadocs, java.sql.Date is intended to
    match with the SQL DATE type, which has date resolution, much less than the
    resolution of the Java type. java.sql.Timestamp matches with the SQL
    TIMESTAMP type, which has fractional-second resolution like the Java type.
    (Except in MySQL.)

    This could be the source of your problem, OP. It could be that the JDBC
    connection is dropping the hours/minutes/seconds/fractions part in the
    conversion to a SQL type. You have not given any information about the SQL
    data type involved, what the DMBS is, what the code that sets the Java value
    looks like or anything else like that that would help us be more exact in our
    answers.

    java.text.DateFormat (and its offspring) should work just fine on
    java.sql.Timestamp. (Bear in mind that "[o]nly integral seconds are stored in
    the java.util.Date component" of java.sql.Timestamp.)

    --
    Lew
    Lew, Jan 22, 2008
    #6
    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,936
    VisionSet
    Dec 2, 2003
  2. mgalaty
    Replies:
    0
    Views:
    2,712
    mgalaty
    Sep 10, 2004
  3. Replies:
    5
    Views:
    98,535
    vongeuca
    May 4, 2011
  4. BoBi
    Replies:
    3
    Views:
    4,146
  5. grz01
    Replies:
    3
    Views:
    2,315
    Arne Vajhøj
    Sep 18, 2009
Loading...

Share This Page