Issue
I am trying to parse my date it works for most of the months but for some reason, it doesn't work for April but on the other hand, it works for juin
My code :
Locale locale = Locale.FRANCE;
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder().appendPattern("dd MMM yyyy (HH:mm)").toFormatter(locale);
LocalDateTime dateTime1 = LocalDateTime.parse(, dateTimeFormatter);
LocalDateTime dateTime2 = LocalDateTime.parse(, dateTimeFormatter);
if(dateTime1.compareTo(dateTime2) < 0) {
System.out.println("true");
} else {
System.out.println("false");
}
My error :
java.time.format.DateTimeParseException: Text '01 avril 2021 (09:40)' could not be parsed at index 3
Is there still something wrong with my pattern
Other Types of months Values:
19 déc. 2019 (21:15)
10 févr. 2020 (07:58)
Solution
You can define multiple optional sections for DateTimeFormatter. Keep in mind that with this configuration also "" or "01 avril 2021 (09:40)01 avr. 2021 (09:40)" would be valid (thanks to @Michael for the clarification).
public class FormatTest {
public static void main(String[] args) {
test("01 avril 2021 (09:40)");
test("01 avr. 2021 (09:40)");
test("19 déc. 2019 (21:15)");
test("19 décembre 2019 (21:15)");
test("10 févr. 2020 (07:58)");
}
private static void test(String value) {
Locale locale = Locale.FRANCE;
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
.appendPattern("[dd MMMM yyyy (HH:mm)][dd MMM yyyy (HH:mm)]").toFormatter(locale);
LocalDateTime dateTime1 = LocalDateTime.parse(value, dateTimeFormatter);
System.out.println(dateTime1);
}
}
Answered By - TomStroemer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.