Cameron
Cameron

Reputation: 185

Convert string to DATE (JSP)

I have a .jsp file (java service pages) with a form that takes in a string date that I needed converted into a sql date datatype.

//Im making a data object
Date userDate = new Date();

//THIS IS THE LINE IM GETTING AN ERROR ON
//Im trying to set the date by getting the form parameter and converting the result to a date       
userDate.setDate(TO_DATE(request.getParameter("date_updated"), "YYYY-MM-DD"));

//and save the date into my database    
ps.setDate(11,userDate);

Any ideas as to what I am doing wrong?

Upvotes: 1

Views: 7429

Answers (3)

Anonymous
Anonymous

Reputation: 86223

Provided that you can use at least Java 8 and at least JDBC 4.2, just do the following:

    // Create a date object holding the date from the form parameter
    LocalDate userDate = LocalDate.parse(request.getParameter("date_updated"));
    // Save the date into the database
    ps.setObject(11, userDate);

Any ideas as to what I am doing wrong?

There are a couple of problems in the line where you got the error.

    userDate.setDate(TO_DATE(request.getParameter("date_updated"), "YYYY-MM-DD"));
  1. userDate.setDate won’t work. This method was originally meant for setting the day of month only. It takes as argument in int in the range from 1 through 31. The method is deprecated, meaning you should not use it. It was deprecated because it works unreliably across time zones.
  2. TO_DATE seems to be an SQL function. You can use SQL functions in SQL and Java methods in Java, but not the other way. Java has other ways of converting strings to dates.

Both java.util.Date and java.sql.Date are long outdated. I recommend you don’t use them except if you cannot avoid a legacy API that requires one of them. An example of the latter would be if you haven’t yet got a JDBC 4.2 compliant JDBC driver (but I believe most of us have). In this case you may convert your LocalDate to java.sql.Date before passing it to your prepared statement, like this:

    ps.setDate(11, java.sql.Date.valueOf(userDate)); // pre-JDBC 4.2 way

Upvotes: 1

NullPointer
NullPointer

Reputation: 7368

This is the simple way of converting string into util date and sql date in java

    String date_updated=request.getParameter("date_updated");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//give format in which you are receiving the `String date_updated`
    java.util.Date date = sdf.parse(date_updated);
    java.sql.Date sqlDate_updated = new java.sql.Date(date.getTime());
    ps.setDate(11, sqlDate_updated); 

Upvotes: 1

ernest_k
ernest_k

Reputation: 45309

You can't invoke an SQL function in Java code. You should perhaps use it in the statement sent to the database, like so:

ps = connection.prepareStatement("update t set val = to_date(?, 'YYYY-MM-DD')");

With that, you can just send the text representation of the date for the conversion to be done in the database (by setting a string on bind variable).

Alternatively, you can parse the date and set a date on the statement:

userDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("date_updated"));
//make java.sql.Date from that and then 
ps.setDate(11,userSqlDate);

Upvotes: 1

Related Questions