开发者

Parsing a String into date with pattern:"dd/MM/yyyy"

开发者 https://www.devze.com 2022-12-28 19:11 出处:网络
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.

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 dateimp开发者_如何学Pythonut=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.


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()))   


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.


where you have:

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

try

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


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));

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号