How to parse case-insensitive strings with jsr310 DateTimeFormatter?

谁说我不能喝 提交于 2019-11-26 20:49:55
amaidment

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

e.g.

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

This alternative is usefull for initializating static variables:

DateTimeFormatter myFormatter = new DateTimeFormatterBuilder()
                               .parseCaseInsensitive()
                               .appendPattern("dd-MMM-yyyy")
                               .toFormatter(Locale.ENGLISH);

Just an extra Note, the order matters.

This is case insensitive:

            DateTimeFormatter format = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .parseLenient()
                .appendPattern("HH:mm EEEE")
                .toFormatter(); 

This is not:

            DateTimeFormatter format = new DateTimeFormatterBuilder()
                .appendPattern("HH:mm EEEE")
                .parseCaseInsensitive()
                .parseLenient()
                .toFormatter(); 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!