Reputation: 1
I am using SimpleDateFormat class in JAVA to convert string value to date format. Below is the code
try{
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date newDate = format.parse(value);
if(newDate != null)
return newDate;
else
return null;
}
My input is convertToDate("12-13-2011","dd-MM-yyyy") my output is Thu Jan 12 00:00:00 IST 2012 Why it is not throwing an error instead it takes next year and month as JAN.
Thanks PradeepKumar
Upvotes: 0
Views: 313
Reputation: 4100
You specified a format in which the first two digits are the days and the second two digits are the month.
Anyway, make sure to call
format.setLenient(false)
if you want a stricter formatting, as without setting this the SimpleDateFormat will try to interpret your date.
Upvotes: 2
Reputation: 64622
That's how SimpleDateFormat
works. If you want to change the behavior extend the class and override the method format to tweak the behavior.
Upvotes: 0