PradeepKumar
PradeepKumar

Reputation: 1

Issue in Date Format in JAVA

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

Answers (3)

manub
manub

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

Boris Pavlović
Boris Pavlović

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

dilbert
dilbert

Reputation: 127

("12-13-2011","dd-MM-yyyy") -> because ther is no 13th month? ;)

Upvotes: 0

Related Questions