please consider the following snippet:
SimpleDateFormat parser = new SimpleDateFormat("MMdd");
parser.setLenient(false);
try {
Date date = parser.parse("0229");
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
I have a text field which should contain a date in the format MMdd (no year, as it should always default to the current year).
This year, 2012, a leap year, I found myself in the strange situation of not being able to parse the valid date February 29th.
The calendar used by the code above always defaults to 1970, which, though luck, was not a leap year. Hence trying to parse “0229” always throws a parse exception.
Any idea on how to parse this?
Append the current year and use a format of “MMddyyyy”?