I'm aware of the concept of "week year" and that parsing 17
using YY
will result in a date of Jan 1, 2017, but switching to 18
will result in Dec 31, 2017 because that is a Sunday, the first day of the week of the next year, 2018.
I don't understand though why when you add in a MM
that it doesn't seem to affect the outcome of the date, but doesn't throw an error either.
Example using JDK 7:
public static void main(String ...strings) throws Exception {
testDateFormat("0218");
testDateFormat("0418");
testDateFormat("0618");
testDateFormat("0818");
testDateFormat("1018");
}
public static void testDateFormat(String baseDate) throws Exception {
String fromFmt = "MMYY";
String toFmt = "MM-dd-yyyy";
SimpleDateFormat sdfSource = new SimpleDateFormat(fromFmt);
SimpleDateFormat sdfDest = new SimpleDateFormat(toFmt);
Date parsedDate = sdfSource.parse(baseDate);
String output = sdfDest.format(parsedDate);
System.out.println(output);
}
Output:
12-31-2017
12-31-2017
12-31-2017
12-31-2017
12-31-2017
Is this expected behavior? I.e. Does Java ignore the first part in the event of week years? I couldn't find any documentation on that at https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html