8 Replies Latest reply: Oct 2, 2008 2:03 PM by 3004 RSS

    Convert a string to date, so that i can insert it on SQL database

    801477
      Hi, me again..

      i have this variable
      date.getText()

      i want to convert it to date, so i did this:

      d = (Date) DateFormat.parse(date.getText());

      but it says "Cannot make a static reference to the non-static method parse(String) from the type DateFormat"

      what the hell is that?
      : \
      can someone help? i want the date to correspond to the variable format on my database (DATETIME (yyyy-mm-dd))
        • 1. Re: Convert a string to date, so that i can insert it on SQL database
          masijade
          Instantiate an instance of DateFormat and call parse using that rather than the Class.
          • 2. Re: Convert a string to date, so that i can insert it on SQL database
            801477
            OK, like this?:

            DateFormat df;

            df = DateFormat.parse(data.getText());

            i did this, but the IDE says the same thing

            : \
            • 3. Re: Convert a string to date, so that i can insert it on SQL database
              3004
              GMX750 wrote:
              can someone help? i want the date to correspond to the variable format on my database (DATETIME (yyyy-mm-dd))
              You don't need to do that. Chances are your database's DATE, TIME, and DATETIME fields don't even have formats, just as Java's Dates don't.

              If you're stating with a String, create an appropriate SimpleDateFormat object to parse() it into a java.util.Date.

              Once you have a java.util.Date, use the appropriate method(s) and constructor(s) to create a java.sql.Date or java.sql.Timestamp.

              Then use PreparedStatement's setDate() or setTimestamp() method.
              • 4. Re: Convert a string to date, so that i can insert it on SQL database
                DrClap
                GMX750 wrote:
                OK, like this?:

                DateFormat df;
                No. That just declares a variable, it doesn't instantiate any objects. Please read the API documentation for SimpleDateFormat and use an object of that class to parse a String to a Date.
                • 5. Re: Convert a string to date, so that i can insert it on SQL database
                  843785
                  Timeout! Shouldn't your table column be of a type such as DATE or TIMESTAMP, not NVARCHAR?

                  edit: too slow. Listen to jverd.

                  Actually, stop and learn Java first. You're trying to do something non-trivial (database access) when you can't do something trivial (call a method). It's like you're trying to perform surgery to remove an appendix and you're holding up the scalpel and saying "which end do I use?"

                  edit #2: too slow, again.
                  • 6. Re: Convert a string to date, so that i can insert it on SQL database
                    3004
                    GMX750 wrote:
                    OK, like this?:

                    DateFormat df;

                    df = DateFormat.parse(data.getText());

                    i did this, but the IDE says the same thing

                    : \
                    If you don't know the difference between static and non-static methods, or how to instantiate an object, you're not ready to be messing with JDBC yet. Sorry if that sounds harsh, but it's the truth. If you keep trying to go down this path without getting a handle on the fundamentals, you're just setting yourself up for more frustration and a poor excuse for an education.

                    Sun's [basic Java tutorial|http://java.sun.com/docs/books/tutorial/]

                    Sun's [New To Java Center|http://java.sun.com/learning/new2java/index.html].Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.

                    [http://javaalmanac.com|http://javaalmanac.com]. A couple dozen code examples that supplement [The Java Developers Almanac|http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance].

                    jGuru. A general Java resource site. Includes FAQs, forums, courses, more.

                    JavaRanch. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.

                    Bruce Eckel's [Thinking in Java|http://mindview.net/Books/DownloadSites] (Available online.)

                    Joshua Bloch's [Effective Java|http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683/ref=pd_bbs_1?ie=UTF8&s=books&qid=1214349768&sr=8-1]

                    Bert Bates and Kathy Sierra's [Head First Java|http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance].

                    James Gosling's [The Java Programming Language|http://www.bookpool.com/sm/0321349806].
                    • 7. Re: Convert a string to date, so that i can insert it on SQL database
                      801477
                      if i do this DateFormat df = new DateFormat(); it doesnt instanciate it because DateFormat is not instanciateable, at least that's what Eclipse says....
                      • 8. Re: Convert a string to date, so that i can insert it on SQL database
                        3004
                        GMX750 wrote:
                        if i do this DateFormat df = new DateFormat(); it doesnt instanciate it because DateFormat is not instanciateable, at least that's what Eclipse says....
                        Seriously. Go back to the basics. You're missing a lot of really fundamental stuff that would make that not a mystery at all.

                        [Formatting a Date Using a Custom Format|http://www.javaalmanac.com/egs/java.text/FormatDate.html]
                        [Parsing a Date Using a Custom Format|http://www.javaalmanac.com/egs/java.text/ParseDate.html]