DateTimeFormatter中的通配符 [英] Wildcard in DateTimeFormatter
问题描述
我需要将字符串解析为LocalDate
.该字符串看起来像用正则表达式表示的31.* 03 2016
(即.*
表示在日期号之后可能有0个或更多的未知字符).
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).
示例输入/输出:31xy 03 2016
==> 2016-03-31
Example input/output: 31xy 03 2016
==> 2016-03-31
我希望在DateTimeFormatter文档中找到通配符语法,以允许使用以下模式:
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"));
但是我什么也找不到.
是否有一种简单的方法来用DateTimeFormatter
表示可选的未知字符?
Is there a simple way to express optional unknown characters with a DateTimeFormatter
?
ps:我显然可以在解析字符串之前对其进行修改,但这不是我要的.
ps: I can obviously modify the string before parsing it but that's not what I'm asking for.
推荐答案
java.time
中没有对此的直接支持.
There is no direct support for this in java.time
.