I need to parse a string into a LocalDate
. The string looks like 31.* 03 2016
in regex terms (i.e. .*
means that there may be 0 or more unknown characters after the day number).
Example input/output: 31xy 03 2016
==> 2016-03-31
I was hoping to find a wildcard syntax in the DateTimeFormatter documentation to allow a pattern such as:
LocalDate.parse("31xy 03 2016", DateTimeFormatter.ofPattern("dd[.*] MM yyyy"));
but I could not find anything.
Is there a simple way to express optional unknown characters with a DateTimeFormatter
?
ps: I can obviously modify the string before parsing it but that's not what I'm asking for.
I’d do it in two steps, use a regexp to get the original string into something that LocalDate can parse, for example:
I know it’s not what you asked for.
There is no direct support for this in
java.time
.The closest would be to use parse(CharSequence,ParsePosition) using two different formatters.
While the above is untested it should basically work. However, it would be far easier parse it manually.