Results 1 to 5 of 5
  1. #1
    bochra is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default Convert String to Date?

    Hi

    I have a string variable like this

    String s1='2010-05-12'
    and I would like to change it to Date format compatable with the date format in MySql database which is also like 'YYYY-MM-dd'

    could you please help ?
    what should I use ????
    I use the following code
    Java Code:
    String s='2010-11-25';
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date s1 =dateFormat.parse(s);
    the output for this code was
    Thu Nov 25 00:00:00 GMT 2010

    it is horrible ! how can I save this date into MySQL (because I want to change the string to date but not this format I want it to be the same format I mentioned above)

    please please help me!!
    Last edited by bochra; 11-14-2010 at 03:54 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't use SQL, but I do know that SimpleDateFormat returns a java.util.Date object, which I believe can be turned into a java.sql.Date object by using the latter's constructor that takes a long parameter.

    Also, please understand that the java.util.Date object that you've created does not have a format. The String that you think is ugly is simply the toString representation of the Date object and nothing more. It may take some effort but you need to separate the idea of Date and String representation of Date as two separate and distinct entities.

  3. #3
    bochra is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    thank you very much

    so could you please tell me how to convert from java.util.Date to java.sql.Date?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, I don't use SQL, but just looking at the API, perhaps you could do

    Java Code:
    java.sql.Date mySqlDate = new java.sql.Date(myUtilDate.getTime());

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

Similar Threads

  1. converting string (GMT date) to date in US time.
    By JRuyechan in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 07:07 AM
  2. convert String to date and pass to PreparedStatement
    By computerbum in forum New To Java
    Replies: 1
    Last Post: 09-21-2010, 03:31 AM
  3. convert String date to Date
    By computerbum in forum New To Java
    Replies: 7
    Last Post: 09-18-2010, 03:26 PM
  4. how we can convert string to date?
    By cowboy2010 in forum New To Java
    Replies: 4
    Last Post: 08-25-2010, 08:41 PM
  5. how to convert date format
    By saran123 in forum New To Java
    Replies: 5
    Last Post: 10-16-2008, 06:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •