I’m trying to use SimpleDateFormat of Java to parse a String to date with the following code.
public class DateTester {
public static void main(String[] args) throws ParseException {
String dateString = "2011-02-28";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
System.out.println(dateFormat.parse(dateString));
}
}
I was expecting some parse error. But interestingly, it prints the following String.
Wed Jul 02 00:00:00 IST 195
Couldn’t reason it out. Can anyone help?
Thanks
SimpleDateFormathas parsed2011as month number 2011, because month (MM) is the first part of the date pattern.If you add 2011 months to year 28, you get year 195.
2011 months is 167 years and 7 months. July is the 7th month. You specified 02 as the day, 28 as the year, 28 + 167 = 195, so
02 July 195is correct.