This discussion is archived
2 Replies Latest reply: Apr 13, 2009 1:27 AM by 807588 RSS

SimpleDateFormat parses wrong date successfully (lenient = false). Why?

807588 Newbie
Currently Being Moderated

I've got a problem validating date by SimpleDateFormat. Format "yyyy" successfully parses string like "2009-78" into date 01.01.2009. Can you please help me with it?

Here is my code:
SimpleDateFormat format = new SimpleDateFormat("yyyy");
String dateStr = "2009-78";
Date date;
try {
    date = format.parse(dateStr);
} catch (ParseException e) {
    date = null;

System.out.println(String.format("String '%s' parsed to date %s", dateStr, date));
String '2009-78' parsed to date Thu Jan 01 00:00:00 MSK 2009
I need an exception to be thrown in such situation. How can I check where the string represents a correct date?

Thanks for your help.

Edited by: su.eug on Apr 13, 2009 12:56 AM