Hi I've got a simple date format set up with a custom format string: MMddyy
and I give it the following value to parse: 4 1 01
I don't think it should parse this because of the spaces but the Simple Date Format is returning the date
April 4th 0001AD
any ideas why?
This is expected behaviour - you are telling the DateFormat object to expect a 6 character String representation of a date and that is what you passed in. Spaces are parsed OK. However, if you used "4x1x01" you would get an error. Note that when parsing, leniency defaults to true e.g.
When leniency is set to true (the default behaviour), the parse makes an effort to decipher invalid input e.g. the 35th day of a 31 day month becomes the 4th day of the next month.
for parsing the size of a pattern (number of repeated characters) is not the expected size of the corresponding text. From the javadoc, for the different relevant presentation types:
The whitespace causes the parser to stop parsing the actual field (trailing spaces are not valid for numbers) and start with the next one. Since the pattern does not have a space between these two fields, it is not consumed and is part of the second field (leading spaces are valid). So the year got is not "exactly two digits" and will not be parsed into the default century.
Parsing tests (
lenient
set tofalse
):The 2 digit year is ambiguous - and it is therefore assuming 0001 - the first year that would have ended in 01. Can you convert to 4 digit years - maybe using String manipulation?