delegating the date parsing to https://github.com/sisyphsu/dateparser
This commit is contained in:
parent
7abc9d1d96
commit
4328c3ca19
11
pom.xml
11
pom.xml
|
@ -262,6 +262,12 @@
|
|||
<version>${dhp.commons.lang.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.sisyphsu</groupId>
|
||||
<artifactId>dateparser</artifactId>
|
||||
<version>1.0.7</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
|
@ -330,6 +336,11 @@
|
|||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.sisyphsu</groupId>
|
||||
<artifactId>dateparser</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
|
|
|
@ -15,6 +15,7 @@ import org.apache.commons.codec.binary.Hex;
|
|||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.DateUtils;
|
||||
|
||||
import com.github.sisyphsu.dateparser.DateParserUtils;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import eu.dnetlib.dhp.schema.oaf.*;
|
||||
|
@ -305,13 +306,7 @@ public class ModelSupport {
|
|||
|
||||
private static final String schemeTemplate = "dnet:%s_%s_relations";
|
||||
|
||||
public static final String[] DATE_TIME_FORMATS = {
|
||||
"yyyy-MM-dd'T'hh:mm:ss.SSS'Z'",
|
||||
"yyyy-MM-dd'T'hh:mm:ss'Z'",
|
||||
"yyyy-MM-dd hh:mm:ss'Z'",
|
||||
"yyyy-MM-dd hh:mm:ss",
|
||||
"yyyy-MM-dd"
|
||||
};
|
||||
public static final String DATE_FORMAT = "yyyy-MM-dd";
|
||||
|
||||
private ModelSupport() {
|
||||
}
|
||||
|
@ -506,8 +501,8 @@ public class ModelSupport {
|
|||
}
|
||||
if (StringUtils.isNotBlank(dateA) && StringUtils.isNotBlank(dateB)) {
|
||||
|
||||
final Date a = tryParse(dateA);
|
||||
final Date b = tryParse(dateB);
|
||||
final Date a = DateParserUtils.parseDate(dateA);
|
||||
final Date b = DateParserUtils.parseDate(dateB);
|
||||
|
||||
if (Objects.nonNull(a) && Objects.nonNull(b)) {
|
||||
return a.before(b) ? dateA : dateB;
|
||||
|
@ -519,14 +514,4 @@ public class ModelSupport {
|
|||
}
|
||||
}
|
||||
|
||||
private static Date tryParse(String date) throws DateTimeParseException {
|
||||
try {
|
||||
return DateUtils.parseDate(date, DATE_TIME_FORMATS);
|
||||
} catch (ParseException e) {
|
||||
// ignore it, try another format
|
||||
}
|
||||
final String formats = String.join("\n", Arrays.asList(DATE_TIME_FORMATS));
|
||||
throw new DateTimeParseException(String.format("cannot parse %s, supported formats: \n%s", date, formats), date, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -108,8 +108,8 @@ public class MergeTest {
|
|||
@Test
|
||||
public void mergeRelationTestParseException() {
|
||||
assertThrows(DateTimeParseException.class, () -> {
|
||||
Relation a = createRel(true, "2016 April 05");
|
||||
Relation b = createRel(true, "2016-04-05");
|
||||
Relation a = createRel(true, "Once upon a time ...");
|
||||
Relation b = createRel(true, "... in a far away land");
|
||||
a.mergeFrom(b);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue