From 1aeb7e2a1778db5604870571b633c478f3746dd7 Mon Sep 17 00:00:00 2001 From: Bernard Tison Date: Sat, 4 May 2019 01:09:56 +0200 Subject: [PATCH] Milliseconds of dates stored in postgresql are trimmed: '2019-05-03T22:15:47.500Z' becomes '2019-05-03T22:15:47.5Z'. The DateFormat parser cannot parse the date when only one digit represents milliseconds. --- .../navy/processviewer/RestApiVerticle.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/redhat/cajun/navy/processviewer/RestApiVerticle.java b/src/main/java/com/redhat/cajun/navy/processviewer/RestApiVerticle.java index 8351b3d..3244983 100644 --- a/src/main/java/com/redhat/cajun/navy/processviewer/RestApiVerticle.java +++ b/src/main/java/com/redhat/cajun/navy/processviewer/RestApiVerticle.java @@ -2,6 +2,8 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.time.temporal.TemporalAccessor; import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -21,6 +23,10 @@ public class RestApiVerticle extends AbstractVerticle { FreeMarkerTemplateEngine engine; + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + + DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.S'Z'"); + @Override public Completable rxStart() { return initializeHttpServer(config()); @@ -94,7 +100,6 @@ private void processInstanceData(RoutingContext rc) { private JsonObject transformProcessData(JsonObject data) { - DateTimeFormatter in = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); DateTimeFormatter out = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String mission = getVariableValue(data.getJsonArray("variables"), "mission"); @@ -105,8 +110,8 @@ private JsonObject transformProcessData(JsonObject data) { .put("instanceId", Long.toString(data.getLong("processinstanceid"))) .put("processId", data.getString("processid")) .put("status", status(data.getInteger("status"))) - .put("startDate", out.format(LocalDateTime.from(in.parse(data.getString("start_date"))))) - .put("endDate", data.getString("end_date") == null ? "" : out.format(LocalDateTime.from(in.parse(data.getString("end_date"))))) + .put("startDate", out.format(LocalDateTime.from(parse(data.getString("start_date"))))) + .put("endDate", data.getString("end_date") == null ? "" : out.format(LocalDateTime.from(parse(data.getString("end_date"))))) .put("duration", data.getLong("duration") == null ? "" : data.getLong("duration") / 1000) .put("assignments_retries", getVariableValue(data.getJsonArray("variables"), "nrAssignments")) .put("responder_id", match(".*responderId=([0-9]*),", mission)) @@ -114,9 +119,6 @@ private JsonObject transformProcessData(JsonObject data) { .put("responder_location",coordinates(".*responderLat=([-+]?[0-9.]*),",".*responderLong=([-+]?[0-9.]*),", mission)) .put("destination_location",coordinates(".*destinationLat=([-+]?[0-9.]*),",".*destinationLong=([-+]?[0-9.]*),", mission)) .put("image", data.getString("image")); - - - //responderId=87 } private String status(int status) { @@ -170,6 +172,16 @@ private String coordinates(String patternLat, String patternLon, String text) { } } + private TemporalAccessor parse(String date) { + TemporalAccessor ta; + try { + ta = dtf.parse(date); + } catch (DateTimeParseException e) { + ta = dtf2.parse(date); + } + return ta; + } + }