Parsing a string for a date with a picture: "dd / MM / yyyy"

I want to insert a date that has this MM / dd / YYYY format, for example: 04/29/2010 - 29/04/2010, which will be inserted into the mysql database in a field with the Date type. So, I have this code:

String dateimput=request.getParameter("datepicker");
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dt = null;
try
{
     dt = df.parse(dateimput);           
     System.out.println("date imput is:" +dt);
} 
catch (ParseException e)
{
     e.printStackTrace();
}

      

but it gives me an error like this:

1-date imput is: Fri May 04 00:00:00 CEST 2012 (this is the wrong value that was entered).

2 -parsing with mysql date type.

I cannot pinpoint the error. Please, help.

+2


a source to share


4 answers


I really don't understand what you are trying to achieve. Analysis of data entry in Date? Storing a date in a MySQL DB field of a date type (or datetime / timestamp) as an object or as a string?

1. Parsing user input

The code you suggest parses the user input to java.util.Date correctly, if the input is indeed in the expected format:

    String dateimput = "24/12/2009"; // Christmas Eve
    java.text.DateFormat df = new java.text.SimpleDateFormat ("dd / MM / yyyy");
    java.util.Date dt = null;
    try
    {
         dt = df.parse (dateimput);           
         System.out.println ("date imput is:" + dt); 
        // = "date imput is: Thu Dec 24 00:00:00 CET 2009"
    } 
    catch (java.text.ParseException e)
    {
         e.printStackTrace ();
    }

Note that when a date knows nothing about the format used to parse it and outputs it like you do ('... + dt'), calls it toString () which by default uses the long date format. However, there is no reason why this should be a problem for you. If you want to register it in a specific format, follow Daniel's guidelines.

2. Storing the date in DB

If you are storing a date in a date / date / time / time field using JDBC, you have two options:



(A) Using String Query and Statement

Construct the insert query as a string and pass it to the Statement, as in:

aConnection.createStatement().executeUpdate(
   "insert into mytable(mydate) values(" + df.format(dt) + ")")

      

In this case, you must ensure that the date string is in a format that the database can understand (for example, yyyy-mm-dd for DB2).

(B) Using PreparedStatement

Or, which should be safer as it prevents SQL injection, and also easier as it delegates the Java type conversion to the desired database form to the JDBC DB driver, use the prepared statement:

PreparedStatement pstmt = con.prepareStatement(
      "insert into mytable(mydate) values(?)");
pstmt.setDate(1, new java.sql.Date(dt.getTime()))   

      

+4


a source


You just need to make sure that the format you are using for parsing is the same as the one used by your datepicker.

UPDATE



On the database side, you just need to use PreparedStatement.setDate () and you don't need to worry about the format.

+1


a source


where do you have:

DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 

      

try

DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 

      

0


a source


when trying DateFormat df = new SimpleDateFormat ("MM / dd / yyyy"); this gives me this: date imput is: Thu Apr 29 00:00:00 CEST 2010 I want to have 04/29/2010 00:00:00

You also need to use DateFormat when outputting Date, otherwise it will just print what is returned by toString (). You can trySystem.out.println(df.format(dt));

0


a source







All Articles