fix year date parsing

new_es_mapping
Enrico Ottonello 3 years ago
parent fbe8af26fa
commit a7a1a466b8

@ -1,6 +1,7 @@
package eu.dnetlib.ariadneplus.reader.utils;
import java.time.LocalDate;
import java.time.Year;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@ -20,10 +21,7 @@ public class ESUtils {
.appendPattern(" yyyy")
.toFormatter(Locale.ROOT);
private static DateTimeFormatter yearOnlyDateFormatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy")
.parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
.toFormatter();
.appendPattern("yyyy").toFormatter();
public static String getESFormatDate(String originalDate) {
try{
@ -31,22 +29,26 @@ public class ESUtils {
return parsedDate.format(elasticSearchDateFormatter);
} catch(Exception e){
try {
LocalDate parsedDate = LocalDate.parse(originalDate, originalRecordDateFormatter);
return parsedDate.format(elasticSearchDateFormatter);
} catch (Exception e1) {
Year year = Year.parse(originalDate);
return year.format(yearOnlyDateFormatter);
} catch (Exception e0) {
try {
LocalDate parsedDate = LocalDate.parse(originalDate.substring(0, 10), elasticSearchDateFormatter);
LocalDate parsedDate = LocalDate.parse(originalDate, originalRecordDateFormatter);
return parsedDate.format(elasticSearchDateFormatter);
} catch (Exception e2) {
} catch (Exception e1) {
try {
return parseBST(originalDate);
} catch (Exception e3) {
return "0000";
LocalDate parsedDate = LocalDate.parse(originalDate.substring(0, 10), elasticSearchDateFormatter);
return parsedDate.format(elasticSearchDateFormatter);
} catch (Exception e2) {
try {
return parseBST(originalDate);
} catch (Exception e3) {
return "0000";
}
}
}
}
}
}
private static String parseBST(String BSTDate) {

@ -20,4 +20,25 @@ public class ESUtilsTest {
String parsed = ESUtils.getESFormatDate(date);
Assert.assertEquals(parsed, "2020-05-15");
}
@Test
public void testYearDate(){
String date = "2012";
String parsed = ESUtils.getESFormatDate(date);
Assert.assertEquals(parsed, "2012");
}
@Test
public void testEmtyDate(){
String date = "";
String parsed = ESUtils.getESFormatDate(date);
Assert.assertEquals(parsed, "0000");
}
@Test
public void testErrorDate(){
String date = "????";
String parsed = ESUtils.getESFormatDate(date);
Assert.assertEquals(parsed, "0000");
}
}

Loading…
Cancel
Save