如何使用jsr310 DateTimeFormatter解析不区分大小写的字符串? [英] How to parse case-insensitive strings with jsr310 DateTimeFormatter?

查看:312
本文介绍了如何使用jsr310 DateTimeFormatter解析不区分大小写的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jsr-310有一个方便的类 DateTimeFormatters ,它允许你构造一个 DateTimeFormatter 。我特别喜欢模式(String)方法 - 参见 javadoc

jsr-310 has a handy class DateTimeFormatters which allows you to construct a DateTimeFormatter. I particularly like the pattern(String) method - see javadoc

然而,我遇到了一个问题,即这是区分大小写的 - 例如

However, I hit a problem whereby this is case sensitive -- e.g.

DateTimeFormatters.pattern("dd-MMM-yyyy");

与01-Jan-2012匹配,但不与01-JAN-2012匹配或01-jan-2012。

matches with "01-Jan-2012", but not with "01-JAN-2012" or "01-jan-2012".

一种方法是断开字符串并解析组件,或者另一种方法是使用Regex替换不区分大小写的字符串区分大小写的字符串。

One approach would be to break the string down and parse components, or another would be to use Regex to replace the case-insensitive strings with the case-sensitive string.

但感觉应该有更简单的方法......

But it feels like there ought to be an easier way...

推荐答案

根据用户指南(离线,请参阅 JavaDoc 代替),你应该使用 DateTimeFormatterBuilder 来构建一个复杂的 DateTimeFormatter

And there is... according to the User Guide (offline, see JavaDoc instead), you should use DateTimeFormatterBuilder to build a complex DateTimeFormatter

例如

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
builder.parseCaseInsensitive();
builder.appendPattern("dd-MMM-yyyy");
DateTimeFormatter dateFormat = builder.toFormatter();

这篇关于如何使用jsr310 DateTimeFormatter解析不区分大小写的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆