diff --git a/src/main/java/com/epam/ta/reportportal/ws/reporting/databind/MultiFormatDateDeserializer.java b/src/main/java/com/epam/ta/reportportal/ws/reporting/databind/MultiFormatDateDeserializer.java index 9b86820..1765d57 100644 --- a/src/main/java/com/epam/ta/reportportal/ws/reporting/databind/MultiFormatDateDeserializer.java +++ b/src/main/java/com/epam/ta/reportportal/ws/reporting/databind/MultiFormatDateDeserializer.java @@ -24,8 +24,12 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeFormatterBuilder; +import java.time.format.DateTimeParseException; +import java.time.temporal.TemporalAccessor; +import java.util.Arrays; +import java.util.List; /** * Deserialization class for parsing incoming dates of different formats. @@ -38,7 +42,20 @@ public class MultiFormatDateDeserializer extends JsonDeserializer { DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("UTC")); private static final DateTimeFormatter LOCAL_DATE_TIME_MS_FORMAT = - DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS").withZone(ZoneId.of("UTC")); + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"); + + private static final DateTimeFormatter LOCAL_DATE_TIME_MS_FORMAT_DATE = + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXX"); + + private static final List PREDEFINED_FORMATS = Arrays.asList( + DateTimeFormatter.RFC_1123_DATE_TIME, + DateTimeFormatter.ISO_OFFSET_DATE_TIME, + DateTimeFormatter.ISO_DATE_TIME, + DateTimeFormatter.ISO_LOCAL_DATE_TIME, + TIMESTAMP_FORMAT, + LOCAL_DATE_TIME_MS_FORMAT, + LOCAL_DATE_TIME_MS_FORMAT_DATE + ); @Override public Instant deserialize(JsonParser parser, DeserializationContext context) throws IOException { @@ -47,7 +64,6 @@ public Instant deserialize(JsonParser parser, DeserializationContext context) th if (parser.getText() == null) { return Instant.ofEpochMilli(longDate); } - // ignore } catch (Exception e) { // ignore } @@ -59,16 +75,17 @@ public Instant deserialize(JsonParser parser, DeserializationContext context) th } String strDate = parser.getText(); - DateTimeFormatter formatter = - new DateTimeFormatterBuilder().appendOptional(DateTimeFormatter.RFC_1123_DATE_TIME) - .appendOptional(DateTimeFormatter.ISO_OFFSET_DATE_TIME) - .appendOptional(DateTimeFormatter.ISO_DATE_TIME) - .appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME) - .appendOptional(TIMESTAMP_FORMAT) - .appendOptional(LOCAL_DATE_TIME_MS_FORMAT) - .toFormatter(); - - return LocalDateTime.from(formatter.parse(strDate)).toInstant(ZoneOffset.UTC); + for (DateTimeFormatter formatter : PREDEFINED_FORMATS) { + try { + TemporalAccessor parsedDate = formatter.parseBest(strDate, ZonedDateTime::from, + LocalDateTime::from); + return parsedDate instanceof ZonedDateTime ? ((ZonedDateTime) parsedDate).toInstant() + : ((LocalDateTime) parsedDate).toInstant(ZoneOffset.UTC); + } catch (DateTimeParseException e) { + // Exception means the text could not be parsed with this formatter, continue with next formatter + } + } + throw new IOException("Unable to parse date: " + strDate); } } diff --git a/src/test/java/com/epam/ta/reportportal/ws/reporting/deserializers/MultiFormatDateDeserializerTest.java b/src/test/java/com/epam/ta/reportportal/ws/reporting/deserializers/MultiFormatDateDeserializerTest.java index 81a70a7..a47fafd 100644 --- a/src/test/java/com/epam/ta/reportportal/ws/reporting/deserializers/MultiFormatDateDeserializerTest.java +++ b/src/test/java/com/epam/ta/reportportal/ws/reporting/deserializers/MultiFormatDateDeserializerTest.java @@ -26,6 +26,7 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -44,10 +45,12 @@ class MultiFormatDateDeserializerTest { @ParameterizedTest @ValueSource(strings = { "2024-03-01T20:24:09.930987Z", - "2024-03-01T20:24:09.930987654Z", + "2024-03-01T20:24:09.930987654", "2024-03-01T20:24:09.930Z", "2024-03-01T20:24:09.930", "2024-03-01T20:24:09.930+00:00", + "2024-03-01T19:24:09.930-01:00", + "2024-03-01T23:24:09.930+0300", "1709324649930" }) void deserializeDates(String strDate) throws IOException {