New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DateTimeFormatter does not parse months as described in JavaDocs #60
Comments
Joda-Time has tended to have more lenient parsing than formatting, this is one example. Its not going to change now (backward compatibility and upcoming JSR-310) but it could be better documented. |
OK but documentation must be improved. Can you add a boolean flag for strict parsing? |
From JSR 310 JavaDoc: "Number: If the count of letters is one, then the value is printed using the minimum number of digits and without padding as per DateTimeFormatterBuilder.appendValue(java.time.temporal.TemporalField). Otherwise, the count of digits is used as the width of the output field as per DateTimeFormatterBuilder.appendValue(java.time.temporal.TemporalField, int)." I fail to find the notion that this is non-strict parsing. SimpleDateFormat explicitly mentions this. |
JSR-310 has explicit strict/lenient/smart modes. Joda-Time has an explicit appendFixedDecimal method on the builder class that can be used to obtain strict parsing. |
Stephen, the method you provided me does not seem to be very intuitive. I have already provided in the pattern 'yyyy-MM' how may digits I want, why do I need to repeat it? Cannot this be done automatically, if I configure a boolean strict on a formatter? |
You use |
I have evaluated the builder and this works: DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
builder.appendFixedDecimal(DateTimeFieldType.year(), 4);
builder.appendLiteral('-');
builder.appendFixedDecimal(DateTimeFieldType.monthOfYear(), 2);
builder.appendLiteral('-');
builder.appendFixedDecimal(DateTimeFieldType.dayOfMonth(), 2);
DateTimeFormatter parser = builder.toFormatter();
LocalDate date = parser.parseLocalDate("2013-9-13");
System.out.println(date); This fails as desired. This has two implications:
DateTimeFormatter isoParser = ISODateTimeFormat.date();
LocalDate date = isoParser.parseLocalDate("2013-9-14");
System.out.println(date); Accepts this input. Shall I raise another issue? |
If you want to raise another issue, you can, however I can't change Adding a A better issue would be the addition of a strict mode, however that would be a big change at this point in Joda-Time's life and not one I'm planning on making. |
This is a pity. I simply do not understand why this issue has not been discovered before. A strict mode would be perfect. At least, what about a |
If you want to create a pull request with a |
Stephen, that's fine. I would create a pull request in time. Are you able to improve the documentation of the |
According the JavaDocs numbers are parsed this way: "Number: The minimum number of digits. Shorter numbers are zero-padded to this amount."
Consider this code:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM");
DateTime dt = fmt.parseDateTime("2013-9");
It does not throw an expection, it silently ignores the fact that a padding zero is necessary. Java's SimpleDateFormat at least documents this inconsistency. Anyway, I would expect parsing to work the very same way as formatting a DateTime.
The text was updated successfully, but these errors were encountered: