String to Date Java

I am trying to convert the string "5/23/14 02:23:24" from a String in Eclipse to a Date to insert into a SQL statement. The code I have is as follows:

String dateAndTime = "5/23/14 02:23:24";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
Date date = sdf.parse(dateAndTime);
long dateLong = date.getTime();
insertStatement.setDate(1, new java.sql.Date(dateLong));

I expect to see

23-MAY-2014 2.23.24.000000000 PM

in my table, but instead I see

23-MAY-2014 12.00.00.000000000 AM

Can anyone shed some light on what I am doing wrong?

Thanks!

Jon Skeet
people
quotationmark

You're calling setDate, which uses a java.sql.Date. That represents just a date, not a date and time.

You should consider using setTimestamp instead, with a java.sql.Timestamp. (There may be other ways of doing it for your specific database, but that's probably the simplest general solution):

long dateLong = date.getTime();
insertStatement.setTimestamp(1, new java.sql.Timestamp(dateLong));

people

See more on this question at Stackoverflow