在我的国家,我们使用英国日期格式。解析器是否可以将使用任何形式的英国日期格式编写的日期字符串转换为有效的 Java 日期对象?
最佳答案
来自评论:
The parser should support any date format we can imagine and even the ones we haven't thought about but still make sense.
这意味着:
14/3/2011
14/03/2011
14/3/11
14 March 2011
14th March 2011
2011-03-14
2011-3-14
March 14, 2011 (just for kicks)
the Fourteenth of March, 2011 (ok, maybe not this one)
NOT: 3/14/2011
如果您想支持所有这些,您有两个选择:
- 迭代一系列特定格式,直到其中一种有效。
- 使用正则表达式编写您自己的自定义解析器。
如果需要,编写您自己的自定义解析器可以更轻松地支持奇怪的变体。
关于java - 一个读取所有英国日期格式的 Java 日期解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32755337/