conversion of string into java.sql.Date


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: conversion of string into java.sql.Date

Hybrid View

  1. #1
    Join Date
    Jun 2005
    Posts
    15

    conversion of string into java.sql.Date

    hi,
    i have a string in this format:-

    String s = year+"-"+e_month+"-"+date;

    and i want to covert it into

    java.sql.Date sqlToday ;

    so that i can save my values in the data base
    i did every thing realted to that like:-

    SimpleDateFormat ts= new SimpleDateFormat("yyyy-MM-dd");
    sqlToday = new java.sql.Date(ts.parse(s).getTime());

    but couldnt get the results
    so any one can help me
    thanks
    Last edited by vikassheelgupta; 08-16-2005 at 07:01 AM. Reason: rectify one errror that i can think once again

  2. #2
    Join Date
    Aug 2005
    Posts
    14
    Quote Originally Posted by vikassheelgupta
    hi,
    i have a string in this format:-

    String s = date+"-"+e_month+"-"+year;

    and i want to covert it into

    java.sql.Date sqlToday ;

    so that i can save my values in the data base
    i did every thing realted to that like:-

    SimpleDateFormat ts= new SimpleDateFormat("yyyy-MM-dd");
    sqlToday = new java.sql.Date(ts.parse(s).getTime());

    but couldnt get the results
    so any one can help me
    thanks
    Your format appears backwards from the way you say your string is.

    Isn't your string Day-Month-Year? So "dd-MM-yyyy".

  3. #3
    Join Date
    Jun 2005
    Posts
    15
    Hi
    i have changed it but the result will be the same
    even i change it before u can tell me
    thanks alot

  4. #4
    Join Date
    Aug 2005
    Posts
    14
    Quote Originally Posted by vikassheelgupta
    Hi
    i have changed it but the result will be the same
    even i change it before u can tell me
    thanks alot
    Well SimpleDateFormat certainly works so the problem is with your code.

    What do you mean it doesn't work?

    There is an error? The date is wrong?

    Please give more details. If you are getting an error then please post the error message.

  5. #5
    Join Date
    Jun 2005
    Posts
    15
    hi
    sorry it didnt work
    i can able to change the format but it is in java.util.Date not in java.sql.Date
    and one more thing thats my code:-
    String s = date+"-"+e_month+"-"+year;

    SimpleDateFormat ts= new SimpleDateFormat("dd-MMM-yyyy");
    SimpleDateFormat ts1= new SimpleDateFormat("dd-MMM-yyyy");
    if(p!=null)
    {
    //sqlToday = ts.parse( s );
    sqlToday = ts.parse( s );
    }
    System.out.println( ts1.format( sqlToday )) ;

    i am unable to store this value in any variable of type java.util.Date or java .sql.Date
    i cant store
    so how can i store it
    thanks alot

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I don't think the conversion is any problem:

    Code:
    static Calendar cal=Calendar.getInstance();
    
    // handles the format yyyy-mm-dd
    private java.sql.Date getDate(String dateString) throws Exception {
      String [] sd=dateString.split("-");
      int year=Integer.parseInt(sd[0].trim());
      int month=Integer.parseInt(sd[1].trim());
      int date=Integer.parseInt(sd[2].trim());
      cal.set(year,month,date);
      java.sql.Date sD=new java.sql.Date(cal.getTimeInMillis());
      return sD;
    }
    Last edited by sjalle; 08-18-2005 at 07:00 AM.
    eschew obfuscation

  7. #7
    Join Date
    Aug 2004
    Posts
    46
    With all due respect, sjalle, I think you'll find your code produces unexpected results. Calendar months are zero-indexed, so the resulting month of your Date object will be off-by-one. For example, if you use "2005-08-18" as your string, the resulting date from your method will show September as the month (Calendar.SEPTEMBER == 8).

    so how can i store it
    It's unclear what you're trying to do. Does the following help?

    Code:
    String s = "18-AUG-2005";
    DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    java.sql.Date sqlToday = new java.sql.Date(df.parse(s).getTime());
    System.out.println(sqlToday); // 2005-08-18
    Formatting a Date Using a Custom Format
    Parsing a Date Using a Custom Format

    Out of curiosity, why are you trying to parse a SQL date from a String? If you need to insert the date into a database, simply use a PreparedStatement and setDate(). I can't really see why else you might be trying to get a java.sql.Date as opposed to just a java.util.Date...
    Last edited by yawmark; 08-18-2005 at 09:35 AM.

  8. #8
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I forgot about that one, sorry.
    Zero-based months is not a new invention, but, add 1 to month and its ok.
    I assumed a numeric month.

    The database problem is mentioned in the first post in this thread.
    eschew obfuscation

  9. #9
    Join Date
    Aug 2004
    Posts
    46
    I forgot about that one, sorry.
    Zero-based months is not a new invention, but, add 1 to month and its ok.
    I assumed a numeric month.
    Adding one is an easy fix, but I'd still recommend parsing the date using DateFormat. If one *does* need to address the individual months, I'd say stick to the Calendar constants (e.g., Calendar.JULY, etc.). These kinds of OBO errors have a tendency to creep into code and be tough to root out.

    The database problem is mentioned in the first post in this thread.
    Yeah, I'm still not sure of what the actual issue is, as "not working" leaves a lot of possibilities...

  10. #10
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I agree that the format parse is the best solution, especially when you know what
    kind of date formats you are dealing with, and Calendar is the best class for
    processing dates, yes.

    My inclination to do a string parse comes from my experience with loading data from
    multiple sources into a content management system I made a few years back. I then
    had to make a date sniffer due to the insane variety of date "formats" that people like to use.

    Since it is a database entry that doesn't work for a date column I think we can
    assume a plain SQLException due to an invalid date.....

    I don't know what OBO errors are though.
    eschew obfuscation

  11. #11
    Join Date
    Aug 2004
    Posts
    46
    My inclination to do a string parse comes from my experience with loading data from
    multiple sources into a content management system I made a few years back. I then
    had to make a date sniffer due to the insane variety of date "formats" that people like to use.
    LOL. I hear you. And to be true, if the purpose is simply to transform one date string to another format, parsing a Date object can be overkill.

    Since it is a database entry that doesn't work for a date column I think we can assume a plain SQLException due to an invalid date.....
    Agreed. It would be nice to know exactly what was wrong with the date; i.e., what was invalid about it.

    I don't know what OBO errors are though.
    Sorry - "Off-By-One", aka "fencepost" errors.

    Regards...

Similar Threads

  1. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 03:32 PM
  2. Replies: 1
    Last Post: 06-05-2001, 07:12 AM
  3. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 10:16 PM
  4. Replies: 0
    Last Post: 06-22-2000, 08:30 AM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 04:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center