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.

4
  • "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. Commented Aug 14, 2015 at 9:47
  • @Jon Skeet Thanks for your valuable information. The return type of DateFormat.format is String, but I want it in type of Date. Commented Aug 14, 2015 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. Commented Aug 14, 2015 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'. Commented Aug 14, 2015 at 11:06

1 Answer 1

1

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

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

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

2 Comments

Thank you very much. I got the answer. I don't know how could I make this silly mistake.
@abhishek You can avoid this kind of mistake by copying an example of working code (from StackOverflow or Tutorial), run it alongside your problem code to verify. Then compare it to your code, tweaking to match until problem is uncovered.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.