Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse case-insensitive strings with jsr310 DateTimeFormatter?

Tags:

java

jsr310

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");

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

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...

like image 830
amaidment Avatar asked May 29 '12 10:05

amaidment


1 Answers

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();
like image 118
amaidment Avatar answered Nov 10 '22 02:11

amaidment