added support for dates like '27-Oct-2022'

This commit is contained in:
Alessia Bardi 2022-11-16 16:31:47 +01:00
parent f2a33d5352
commit fa5bce3c39
2 changed files with 18 additions and 1 deletions

View File

@ -15,6 +15,7 @@ public class ESUtils {
private static DateTimeFormatter originalRecordDateFormatter = DateTimeFormatter.ofPattern("dd MMM yyyy");
private static DateTimeFormatter elasticSearchDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static DateTimeFormatter fastCatDateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
private static ZoneId preferredZone = ZoneId.of("Europe/London");
private static DateTimeFormatter BST_FORMATTER = new DateTimeFormatterBuilder()
.appendPattern("EEE MMM dd HH:mm:ss ")
@ -24,6 +25,8 @@ public class ESUtils {
private static DateTimeFormatter yearOnlyDateFormatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy").toFormatter();
private static DateTimeFormatter stringMonthDateFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
public static String getESFormatDate(String originalDate) {
try{
LocalDate parsedDate = LocalDate.parse(originalDate, elasticSearchDateFormatter);
@ -48,7 +51,12 @@ public class ESUtils {
LocalDate parsedDate = LocalDate.parse(originalDate, fastCatDateFormatter);
return parsedDate.format(elasticSearchDateFormatter);
} catch (Exception e4) {
return "0000";
try {
LocalDate parsedDate = LocalDate.parse(originalDate, stringMonthDateFormatter);
return parsedDate.format(elasticSearchDateFormatter);
} catch (Exception e5) {
return "0000";
}
}
}
}

View File

@ -41,4 +41,13 @@ public class ESUtilsTest {
String parsed = ESUtils.getESFormatDate(date);
Assert.assertEquals(parsed, "0000");
}
@Test
public void testDateMonthString(){
String date = "27-Oct-2022";
String parsed = ESUtils.getESFormatDate(date);
System.out.println(parsed);
Assert.assertEquals(parsed, "2022-10-27");
}
}