SimpleDateFormat Behavior

I have the following lines:

final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d = simpleDateFormat.parse("2004-52-05");

I expect the exception to be thrown on line 2 because "52" is not a valid month, but the code is executed, and the date stored in object d,

Sat Apr 05 00:00:00 EEST 2008

Can someone explain to me why?

+4
source share
1 answer

If you want to create a date object that strictly matches your pattern, then set lenient to false.

From Javadoc

, . , , . get(), . , GregorianCalendar MONTH == , DAY_OF_MONTH == 32 as 1 .

lenitent

, .

simpleDateFormat.setLenient(false);

Exception, .

java.text.ParseException: Unparseable date: "2004-52-05"
+9

All Articles