I have some text
that is only being parsed by a DateTimeFormatter
when the parse style is Strict
- and not when it's Lenient
.
This seems like the opposite behaviour to what I'd expect?
Example:
String pattern = "ddMMyyHH:mm:ss";
String text = "02011104:21:32";
System.out.println(MessageFormat.format("Strict - {0}", new DateTimeFormatterBuilder().parseStrict().appendPattern(pattern).toFormatter().parse(text)));
System.out.println(MessageFormat.format("Lenient - {0}", new DateTimeFormatterBuilder().parseLenient().appendPattern(pattern).toFormatter().parse(text)));
Output:
Strict - {},ISO resolved to 2011-01-02T04:21:32
Exception in thread "main" java.time.format.DateTimeParseException: Text '02011104:21:32' could not be parsed at index 8
1104
) even thoughyy
should be for 2-digit year of era. Was that supposed to happen? Because of an error in the docs I cannot tell. Appendingyy
inappendPattern
should be equivalent to callingappendValueReduced(ChronoField.YEAR_OF_ERA, 2, 2000)
, but there is no three-argappendValueReduced
method! – TumefacientparseLenient
orparseStrict
? IMHO the documentation is very unclear about what difference it should make. For the recordLocalDateTime.parse(text, DateTimeFormatter.ofPattern(pattern).withResolverStyle(ResolverStyle.LENIENT))
works (but you probably had a reason for wanting to use a builder). – TumefacientappendValueReduced(ChronoField.YEAR_OF_ERA, 2, 2, 2000)
, I get the same behaviour as yours, so it works withoutparseLenitent
but not with. It sounds to me like a bug, and you may consider filing it as such. – TumefacientwithResolverStyle
– Vachill