Converting java.util.Date to java.sql.Date

Discussion in 'Java' started by mail4sushovan@gmail.com, Sep 9, 2006.

  1. Guest

    Hello Friends
    In my program i am using the following codes:

    Date today = new Date();
    log.debug("today is"+today);
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-mm-dd");
    String todayStr = fmt.format(today);
    log.debug("todayStr value"+todayStr);
    java.sql.Date dt = java.sql.Date.valueOf(new String(todayStr));
    log.debug("current Dt is:"+dt);

    The output iam getting is like this:
    today is Sat Sep 09 19:20:13 IST 2006(from java.util.Date)
    todayStr value 2006-20-09(from string value)
    current Dt is: 2007-08-09(from java.sql.Date)


    So how to avoid this.Please help me
    Thank You in advance
    sushovan
    , Sep 9, 2006
    #1
    1. Advertising

  2. <> wrote:
    > In my program i am using the following codes:
    >
    > Date today = new Date();
    > log.debug("today is"+today);
    > SimpleDateFormat fmt = new SimpleDateFormat("yyyy-mm-dd");
    > String todayStr = fmt.format(today);
    > log.debug("todayStr value"+todayStr);
    > java.sql.Date dt = java.sql.Date.valueOf(new String(todayStr));
    > log.debug("current Dt is:"+dt);
    >
    > The output iam getting is like this:
    > today is Sat Sep 09 19:20:13 IST 2006(from java.util.Date)
    > todayStr value 2006-20-09(from string value)
    > current Dt is: 2007-08-09(from java.sql.Date)
    >
    >
    > So how to avoid this.Please help me


    java.util.Date today = new java.util.Date();
    long t = today.getTime();
    java.sql.Date dt = new java.sql.Date(t);

    See also the API doc of java.sql.Date, especially its constructor.
    --
    Thomas
    Thomas Fritsch, Sep 9, 2006
    #2
    1. Advertising

  3. wrote:
    > Hello Friends
    > In my program i am using the following codes:
    >
    > Date today = new Date();
    > log.debug("today is"+today);
    > SimpleDateFormat fmt = new SimpleDateFormat("yyyy-mm-dd");
    > String todayStr = fmt.format(today);
    > log.debug("todayStr value"+todayStr);
    > java.sql.Date dt = java.sql.Date.valueOf(new String(todayStr));
    > log.debug("current Dt is:"+dt);
    >
    > The output iam getting is like this:
    > today is Sat Sep 09 19:20:13 IST 2006(from java.util.Date)
    > todayStr value 2006-20-09(from string value)
    > current Dt is: 2007-08-09(from java.sql.Date)


    As shown by Thomas there is an easier and better way to get an
    java.sql.Date from java.util.Date.

    In your code example you used the wrong date format: 'mm' stands for the
    minutes of the time part, 'MM' is the format for the month of the date.
    Frank Langelage, Sep 9, 2006
    #3
  4. Hesham

    Joined:
    Dec 9, 2008
    Messages:
    1
    I have small problem in program
    how to convert
    java.sql.Date date = new java.sql.Date(today.getTime());
    to Normal String
    any one help me pls in this problem
    Hesham, Dec 9, 2008
    #4
  5. thhui

    Joined:
    May 4, 2011
    Messages:
    1
    String datestr001 = (new java.sql.Date(today.getTime())).toString();


    The corrected one should be:-

    Date today = new Date();
    log.debug("today is"+today);
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
    String todayStr = fmt.format(today);
    log.debug("todayStr value"+todayStr);
    java.sql.Date dt = java.sql.Date.valueOf(new String(todayStr));
    log.debug("current Dt is:"+dt);

    The output is:-
    todayStr=dt
    thhui, May 4, 2011
    #5
  6. vongeuca

    Joined:
    May 4, 2011
    Messages:
    4
    Location:
    california
    Hi everyone, I newbie and I very happy join in forum.[​IMG]
    I hope can be friends with you all members, Thanks.
    I like it this thread.
    vongeuca, May 4, 2011
    #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. BoBi
    Replies:
    3
    Views:
    4,147
  4. Replies:
    5
    Views:
    5,578
  5. grz01
    Replies:
    3
    Views:
    2,315
    Arne Vajhøj
    Sep 18, 2009
Loading...

Share This Page