1

I'm trying to put a user provided date into an SQL database, and I have the following lines to process the string and convert it to a java.sql.Timestamp object.

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm");
java.util.Date badDate = formatter.parse(request.getParameter("datetime")); 
Timestamp date = new Timestamp(badDate.getTime());

The issue is, badDate is the correct date that the user input, but date always gets set to the wrong month and day, usually January 2, and the correct time and year. I had the same problem when I tried to convert to a java.sql.Date object, but then the time was set to midnight as well. I couldn't find anyone with a similar problem after some searching, maybe someone here has seen something like this?

1
  • When you end up with January 2, what was the date you expected? What was the value returned by request.getParameter("datetime")? Are you basing it upon the values actually getting stored in the database, or are you getting the resultant bad date by just printing out the java.sql.Timestamp before you ever go to the database? Commented May 3, 2011 at 3:38

2 Answers 2

8

The date format is wrong, it should be yyyy-MM-dd'T'HH:mm.

Please refer this document for details about date format.

m stands for Minutes
M for Months
h for hours in am/pm (1-12)
H for hours in 24 hour clock

It will be better if you can give a sample of the date value you are trying to parse.

Sign up to request clarification or add additional context in comments.

Comments

1

You want "yyyy-MM-dd'T'hh:mm" (note capitalized MM). Otherwise your month is wrong in the format string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.