How to parse case-insensitive strings with jsr310 DateTimeFormatter?

datetimeformatterbuilder
java-time format datetimeparseexception
could not be parsed, unparsed text found at index 10

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

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

How to parse case insensitive strings using jsr310 , jsr-310 has a handy class DateTimeFormatters which allows you to construct a DateTimeFormatter. I particularly like the pattern(String) method  jsr-310 has a handy class DateTimeFormatters which allows you to construct a DateTimeFormatter. I particularly like the pattern(String) method - see javadoc However

This alternative is usefull for initializating static variables:

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

Feature request: Support case-insensitive `LocalDate` formats , How to parse case insensitive strings using jsr310 DateTimeFormatter? jsr-310 has a convenient DateTimeFormatters class that allows you to  If a month is in UPPER or lower case, i.e. not Title case, DateTimeFormatter cannot parse the date. Is there a simple way to convert a date to title case, or a way to make the formatter more lenie

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

DateTimeFormatter (Java SE 9 & JDK 9 ), value of type `java.time.LocalDate` from String "22-JUL-1974": Failed to deserialize java. at com.fasterxml.jackson.datatype.jsr310.deser. DateTimeFormatter.parse(DateTimeFormatter.java:1851) at java.time. I'd like to propose a new feature in Jackson to support case-insensitive parsing. Here is another example that uses the pre-defined formatter BASIC_ISO_DATE from DateTimeFormatter to parse a string into a LocalDate: // string in basic ISO-8601 format String str = "20191222"; // parse string to date LocalDate date = LocalDate.parse(str, DateTimeFormatter.BASIC_ISO_DATE); Convert String to LocalTime

User amaidment, public final class DateTimeFormatter extends Object LocalDate date = LocalDate.now(); String text = date.format(formatter); Parsing is case insensitive. Java 8 added a new API for working with date and time values (JSR-310). This post shows various examples how the new date/time API can be used.

DateTimeFormatter.java - Source, jsr-310 has a handy class DateTimeFormatters which allows you to construct a DateTimeFormatter. I particularly like the pattern(String) method - see javadoc  If the offset has seconds then they will be handled even though this is not part of the ISO-8601 standard. Parsing is case insensitive. The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the STRICT resolver style.

New src/share/classes/java/time/format/DateTimeFormatter.java, 20 How to parse case-insensitive strings with jsr310 DateTimeFormatter? 17 Changes in access of variables for generic classes in Java 7 · 16 How to parse  3. DateTimeFormatter with Locale. Sometimes we may have dates in specific locales such as french e.g. 29-Mar-2019 will be written in french as 29-Mars-2019.To parse such dates, use DateTimeFormatter withLocale() method to get the formatter in that locale and parse the dates.

Comments
  • Slightly neater third line: builder.appendPattern("dd-MMM-yyyy");
  • Seems to duplicate the accepted Answer from a couple years earlier