I spent most of yesterday afternoon getting a string pulled in via a
request.getParameter properly typed so that it could be updated or
inserted into our Sybase backend. I searched the archives and wasn't
able to find anything that specifically talked about how to do this,
so I wanted to share.


// Initialize to Jan 1, 1901
java.sql.Date sqlDate = new java.sql.Date(1, 0, 1);

String tmpDateStr = request.getParameter("releaseDate");

try {
        // executeUpdate fails on date fields unless they are in
        // yyyy-MM-dd format (at least on our setup. YMMV.)
        SimpleDateFormat sdfTemplate = new SimpleDateFormat("yyyy-MM-dd");

        // Going from string to java.util.Date because that is what
        // format() expects
        java.util.Date nptDate = new java.util.Date(tmpDateStr);

        // Format returns a string, which is what we want for our next
        // statment.
        tmpDateStr = sdfTemplate.format(nptDate);

        // Finally put it into the form needed by our
        // preparedStatement.setDate()
        sqlDate = java.sql.Date.valueOf(tmpDateStr);
}
catch (Exception ex) {
        // Exception parsing code....
}
finally {
        // Whatever
}

So the pattern is String --> java.util.Date --> String -->
java.sql.Date. Hope this helps someone.


--

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to