0

I am getting a date by ajax in String format. But it is getting changed when I am converting it to date by SimpleDateFormat. The month is always changed to Jan. I am worried only about the month change.My code is given below

String appointmentDate = request.getParameter("appointmentDate");
System.out.println(" appointment date in String format "+appointmentDate);

Here I am getting the date correctly(16/12/2015). But when I am changing it to Date format it is getting changed(Fri Jan 16 00:12:00 IST 2015). Whatever I input the month, say August, May, June, I am always getting month Jan.

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
Date parsedDate = dateFormat.parse(appointmentDate);
System.out.println(" appointment date in DATE format "+parsedDate);

Please help me out. Thanks in advance.

abhishek
  • 51
  • 2
  • 11
  • "But when I am changing it to Date format it is getting changed(Fri Jan 16 00:12:00 IST 2015)." You appear to think a `java.util.Date` has a format. It doesn't. If you want to format it in a particular way, use `DateFormat.format`. – Jon Skeet Aug 14 '15 at 09:47
  • @Jon Skeet Thanks for your valuable information. The return type of DateFormat.format is String, but I want it in type of Date. – abhishek Aug 14 '15 at 10:38
  • Then you already have it as a date, but you're complaining about the *formatting* ("But when I am changing it to Date format it is getting changed(Fri Jan 16 00:12:00 IST 2015)") which means you're complaining about a *string* representation. Or were you only worried about the month changing? I've reopened the question, but it would help if you would edit the question to be clear about the problem. – Jon Skeet Aug 14 '15 at 10:55
  • Yes you are right. I am only worried about the change of month. But the problem has been solved now. I put 'dd/mm/yyyy' instead of 'dd/MM/yyyy'. – abhishek Aug 14 '15 at 11:06

1 Answers1

0

As per the JavaDoc, lower case m denotes minutes, not months.

Changing your expression to dd/MM/yyyy should fix the issue.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • Thank you very much. I got the answer. I don't know how could I make this silly mistake. – abhishek Aug 14 '15 at 10:40
  • @abhishek You can avoid this kind of mistake by copying an example of working code (from StackOverflow or [Tutorial](http://docs.oracle.com/javase/tutorial/datetime/TOC.html)), run it alongside your problem code to verify. Then compare it to your code, tweaking to match until problem is uncovered. – Basil Bourque Aug 14 '15 at 16:55