From d3cb923f24462e845eff47aa5782e6fd74684d8f Mon Sep 17 00:00:00 2001 From: Claudio Atzori Date: Thu, 11 Mar 2021 12:37:33 +0100 Subject: [PATCH] introduced java8-based date parsing --- .../java/eu/dnetlib/dhp/schema/common/ModelSupport.java | 9 +++++---- .../test/java/eu/dnetlib/dhp/schema/oaf/MergeTest.java | 5 +++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/common/ModelSupport.java b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/common/ModelSupport.java index 0c7903137d..a92e11b5a3 100644 --- a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/common/ModelSupport.java +++ b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/common/ModelSupport.java @@ -5,6 +5,9 @@ import static com.google.common.base.Preconditions.checkArgument; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAccessor; import java.util.Date; import java.util.Map; import java.util.Objects; @@ -477,8 +480,6 @@ public class ModelSupport { return ((OafEntity) t).getId(); } - public static final String ISO8601FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ"; - public static String oldest(String dateA, String dateB) throws ParseException { if (StringUtils.isBlank(dateA)) { @@ -489,8 +490,8 @@ public class ModelSupport { } if (StringUtils.isNotBlank(dateA) && StringUtils.isNotBlank(dateB)) { - final Date a = new SimpleDateFormat(ISO8601FORMAT).parse(dateA); - final Date b = new SimpleDateFormat(ISO8601FORMAT).parse(dateB); + final Date a = Date.from(Instant.from(DateTimeFormatter.ISO_INSTANT.parse(dateA))); + final Date b = Date.from(Instant.from(DateTimeFormatter.ISO_INSTANT.parse(dateB))); return a.before(b) ? dateA : dateB; } else { diff --git a/dhp-schemas/src/test/java/eu/dnetlib/dhp/schema/oaf/MergeTest.java b/dhp-schemas/src/test/java/eu/dnetlib/dhp/schema/oaf/MergeTest.java index 6ee5b9d859..f5b9bf028f 100644 --- a/dhp-schemas/src/test/java/eu/dnetlib/dhp/schema/oaf/MergeTest.java +++ b/dhp-schemas/src/test/java/eu/dnetlib/dhp/schema/oaf/MergeTest.java @@ -85,6 +85,11 @@ public class MergeTest { b = createRel(true, "2016-04-05T12:41:19.202Z"); a.mergeFrom(b); assertEquals("2016-04-05T12:41:19.202Z", a.getValidationDate()); + + a = createRel(true, "2016-05-07T12:41:19.202Z"); + b = createRel(true, "2016-04-05T12:41:19.202Z"); + a.mergeFrom(b); + assertEquals("2016-04-05T12:41:19.202Z", a.getValidationDate()); } @Test