tags:

views:

195

answers:

4

Hi, I want to insert a date having this format MM/dd/YYYY for example:04/29/2010 to 29/04/2010 to be inserted into mysql database in a field typed Date. 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 those error:

1-date imput is:Fri May 04 00:00:00 CEST 2012 (it is not the correct value that have been entered).

2-dismatching with mysql date type.

I can not detect the error exactly. Please help.

A: 

where you have:

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

try

DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
Luis
when trying DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); it gives me this:date imput is:Thu Apr 29 00:00:00 CEST 2010
kawtousse
I want to have 29/04/2010 00:00:00
kawtousse
Thu Apr 29 00:00:00 CEST 2010 is the date same as 29/04/2010 00:00:00, only formatted differently.
Maurice Perry
But the value formatted will be inserted in database under this formatdd/MM/yyyy 00:00:00 so it will not be accepted like a valis value.
kawtousse
You don't need to worry about the format to insert a date in a database (se my updated post)
Maurice Perry
+1  A: 

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

UPDATE

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

Maurice Perry
A: 

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

When outputting the Date you also need to use DateFormat, otherwise it will just print what is returned by toString(). You could try System.out.println(df.format(dt));

Daniel
+3  A: 

I do not really understand what you are trying to achieve. Parsing user input into a Date? Storing a Date into a MySQL DB field of type date (or datetiteme/timestamp) as an object or as a string?

1. Parsing user input

The code you propose parses user input into a java.util.Date correctly provided that 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();
    }

Notice that when a Date knows nothing about the format used for parsing it and outputting it as you do ('...+dt') calls its toString(), which by default uses the long date format. However there is no reason why that should be a problem for you. If you want to log it in a particular format, follow Daniel's suggestions.

2. Storing the date to the DB

If you store the date into a date /datetime/timestamp field via JDBC you have two options:

(A) Using string query and a Statement

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

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

In this case you must make sure that the date string is in a format the DB can understand (such as yyyy-mm-dd for DB2).

(B) Using a PreparedStatement

Or, which is must safer because it prevents SQL injection, and also easier because it delegates the conversion of java types to the proper database form to the DB's JDBC driver, use a prepared statement:

PreparedStatement pstmt = con.prepareStatement(
      "insert into mytable(mydate) values(?)");
pstmt.setDate(1, new java.sql.Date(dt.getTime()))   
Jakub Holý
yes I want both parsing the string and inserting in database.
kawtousse
I use MYSQL DataBase So this is my query: String query = "Insert into dailytimesheet(trackingDate,activity,projectCode) values ("+df.format(dt)+", \""+activity+",\""+projet+"\")";
kawtousse
Well, it's normally preferred to use PreparedStatement instead of manually composing an SQL string - see e.g. "Understanding Prepared Statements in Oracle" (http://chrisgatesconsulting.com/preparedStatements.ppt) - but there is no rule that can't be broken :)
Jakub Holý