java.sql.Date truncated when written to DB. How to obtain full date?

Discussion in 'Java' started by ddog, Feb 8, 2007.

  1. ddog

    ddog Guest

    I'm trying to write a java.sql.Date to a database and the minutes/
    seconds etc. are not being written. I've seen and tested many examples
    found via searches and still have come up with nothing. Here's what I
    have:

    java.sql.Date formation - recommended forms:
    java.sql.Date entryDttm = new
    java.sql.Date(System.currentTimeMillis());
    OR
    java.sql.Date entryDttm = new java.sql.Date(new
    java.util.Date().getTime());

    // prepared statement insert
    pstmt.setDate(1, entryDttm);
    // what is written to database
    2007-02-07 12:00:00.000

    // what needs to be written
    2007-02-08 09:37:25.793

    The field is stored in a MS SQL Server 8.0 database and is defined as
    a 'datatime' field. Using JDK 1.5.

    What do I need to do to have the minutes/seconds/milliseconds written?
    ddog, Feb 8, 2007
    #1
    1. Advertising

  2. ddog

    Rayle Guest

    not sure but i didi some of this with a school project.. double check
    your mysql syntax on date time... i believe that it is 16 digits in
    length... 4 for year 2 for month etc so it is only 2 for milliseconds
    and if the format is incorrect it uses the default value of
    12:00:00:00

    should be in the mysql manual... i am almost positive i am correct
    Rayle, Feb 8, 2007
    #2
    1. Advertising

  3. On Feb 8, 7:45 am, "ddog" <> wrote:
    > I'm trying to write a java.sql.Date to a database and the minutes/
    > seconds etc. are not being written. I've seen and tested many examples
    > found via searches and still have come up with nothing. Here's what I
    > have:
    >
    > java.sql.Date formation - recommended forms:
    > java.sql.Date entryDttm = new
    > java.sql.Date(System.currentTimeMillis());
    > OR
    > java.sql.Date entryDttm = new java.sql.Date(new
    > java.util.Date().getTime());
    >
    > // prepared statement insert
    > pstmt.setDate(1, entryDttm);
    > // what is written to database
    > 2007-02-07 12:00:00.000
    >
    > // what needs to be written
    > 2007-02-08 09:37:25.793
    >
    > The field is stored in a MS SQL Server 8.0 database and is defined as
    > a 'datatime' field. Using JDK 1.5.
    >
    > What do I need to do to have the minutes/seconds/milliseconds written?


    Can you try using TimeStamp instead? I've not used Dates, but used
    TimeStamps and have never had a problem. Another thing to try would be
    to use sysdate( ) or now( ) in the jdbc statement - this will put the
    database time in the field.

    Also. make sure the field type is DateTime or TimeStamp in MySQL.

    -cheers,
    Manish
    Manish Pandit, Feb 8, 2007
    #3
  4. ddog

    Lew Guest

    Re: java.sql.Date truncated when written to DB. How to obtain fulldate?

    ddog wrote:
    > I'm trying to write a java.sql.Date to a database and the minutes/
    > seconds etc. are not being written. I've seen and tested many examples


    You multi-posted this one to comp.databases as "Re: java.sql.Date value
    truncated using JDBC in MS-SQL Server".

    Follow-up set to cljp. Please cease multi-posting. Use cross-posting if you
    really want multiple groups.

    Andrew - you missed this one?

    - Lew
    Lew, Feb 9, 2007
    #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. Steve C. Orr, MCSD
    Replies:
    1
    Views:
    556
    reaway lee
    Aug 24, 2003
  2. Replies:
    5
    Views:
    98,582
    vongeuca
    May 4, 2011
  3. Lew
    Replies:
    0
    Views:
    465
  4. Replies:
    5
    Views:
    5,595
  5. KaiWen
    Replies:
    102
    Views:
    2,709
    Jorgen Grahn
    Sep 15, 2011
Loading...

Share This Page