How to parse case-insensitive strings with JSR-310 DateTimeFormatter?
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 handle upper or lower case in JSR 310?
DateTimeFormatter
s are strict and case sensitive by default. Use a DateTimeFormatterBuilder
and specify parseCaseInsensitive()
to parse case insensitive.
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("yy-MMM-dd")
.toFormatter(Locale.US);
To be able to parse numeric months (ie. "15-01-12"
), you also need to specify parseLenient()
.
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseLenient()
.appendPattern("yy-MMM-dd")
.toFormatter(Locale.US);
You can also be more verbose to specify only the month part as case insensitive/lenient:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("yy-")
.parseCaseInsensitive()
.parseLenient()
.appendPattern("MMM")
.parseStrict()
.parseCaseSensitive()
.appendPattern("-dd")
.toFormatter(Locale.US);
In theory, this could be faster, but I'm not sure if it is.
PS: If you specify parseLenient()
before the year part, it will also parse 4 digit years (ie. "2015-JAN-12"
) correctly.
While parsing date with upper case month name I get java.time.format.DateTimeParseException
you can configure your DateTimeFormatter
to ignore cases
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("dd-MMM-yy")
.toFormatter(Locale.getDefault());
java.time.format.DateTimeParseException: Text could not be parsed at index 3
The following code works. The problem is you are using "JAN" instead of "Jan".
DateTimeFormatter does not recognize that it seems. and also change the pattern to
"d-MMM-yyyy".
String date1 ="01-Jan-2017";
String date2 = "02-Feb-2017";
DateTimeFormatter df = DateTimeFormatter.ofPattern("d-MMM-yyyy");
LocalDate d1 = LocalDate.parse(date1, df);
LocalDate d2 = LocalDate.parse(date2, df);
Long datediff = ChronoUnit.DAYS.between(d1,d2);
Source: https://www.mkyong.com/java8/java-8-how-to-convert-string-to-localdate/
JSR310 Year.parse() throws DateTimeParseException with values 1000
needs to be padded to 4 digits
Year.parse(String.format("%04d", i));
Parsing year: 2000
Parsing year: ....
Parsing year: 4
Parsing year: 3
Parsing year: 2
Parsing year: 1
Parsing year: 0
If you wanted to parse before year 0, you could use
Year.parse("-0001");
Java 8 DateTimeFormatter for month in all CAPS not working
Answering this question because most of us might not know JSR 310. Hence would search for java 8 LocalDateTime ignore case.
@Test
public void testDateFormat(){
DateTimeFormatter formatter= new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd-MMM-yyyy HH:mm:ss").toFormatter();
LocalDateTime dateTime = LocalDateTime.parse("04-NOV-2015 16:00:00", formatter);
System.out.println(dateTime.getYear());
}
**UPDATE*
To locale
DateTimeFormatter parser = new DateTimeFormatterBuilder().parseCaseInsensitive() .appendPattern("dd-MMM-yyyy HH:mm:ss.SS").toFormatter(Locale.ENGLISH)
java.time.format.DateTimeParseException for dd-MMM-yyyy format
The reason is that parsing is case sensitive by default and the formatter doesn't recognize "jan"
. It would only recognize "Jan"
.
You can construct a case-insensitive parser by using a DateTimeFormatterBuilder
and calling parseCaseInsensitive()
:
Changes the parse style to be case insensitive for the remainder of the formatter.
Parsing can be case sensitive or insensitive - by default it is case sensitive. This method allows the case sensitivity setting of parsing to be changed.
DateTimeFormatter dTF =
new DateTimeFormatterBuilder().parseCaseInsensitive()
.appendPattern("dd-MMM-yyyy")
.toFormatter();
Multiple case insensitive strings replacement
You can try to use regex to archive it
Example
String str = "Dang DANG dAng dang";
//replace all dang(ignore case) with Abhiskek
String result = str.replaceAll("(?i)dang", "Abhiskek");
System.out.println("After replacement:" + " " + result);
Result:
After replacement: Abhiskek Abhiskek Abhiskek Abhiskek
EDIT
String[] old = {"ABHISHEK","Name"};
String[] nw = {"Abhi","nick name"};
String s="My name is Abhishek";
//make sure old and nw have same size please
for(int i =0; i < old.length; i++) {
s = s.replaceAll("(?i)"+old[i], nw[i]);
}
System.out.println(s);
Result:
My nick name is Abhi
Basic ideal: Regex ignore case and replaceAll()
From the comment @flown (Thank you) you need to use
str.replaceAll("(?i)" + Pattern.quote(old[i]), nw[i]);
Because regex treats some special character with a different meaning, ex: .
as any single character
So using the Pattern.quote
will do this.
Related Topics
What Does Biginteger Having No Limit Mean
How to Convert a Java Object (Bean) to Key-Value Pairs (And Vice Versa)
How to Read Xml Response from a Url in Java
Are There Best Practices for (Java) Package Organization
Can Constructor Return a Null Object
Jtable How to Change Background Color
What Are Good Installanywhere Replacements for Installing a Java Ee Application
Does the Sequence of the Values Matter in a JSON Object
How to Add a Utf-8 Bom in Java
How to Make a Jbutton in a Jtable Cell Click-Able
Java Sax Parser Split Calls to Characters()
Exception Thrown Inside Catch Block - Will It Be Caught Again
How to Insert a Character in a String at a Certain Position
Calling a @Bean Annotated Method in Spring Java Configuration
What's in an Eclipse .Classpath/.Project File
Uses for the Java Void Reference Type