diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampType.java b/src/main/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampType.java index 57b9086..4f97788 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampType.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampType.java @@ -21,6 +21,7 @@ import java.time.LocalDateTime; import java.util.Calendar; import java.util.Map; +import java.util.TimeZone; /** * Supports returning a java.sql.Timestamp from a String in the @@ -73,6 +74,19 @@ public java.sql.Timestamp asTimestamp(String value, Calendar calendar) throws SQ if (value.length() > 11 && value.charAt(10) == 'T') { value = value.replace('T', ' '); } + // Timestamp.valueOf() does not like timezone information + if (value.length() > 23) { + if (value.length() == 24 && value.charAt(23) == 'Z') { + value = value.substring(0, 23); + } + else if (value.charAt(23) == '+' || value.charAt(23) == '-') { + // 'calendar' parameter takes precedence + if (calendar == null) { + calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT" + value.substring(23))); + } + value = value.substring(0, 23); + } + } if (calendar == null) { return Timestamp.valueOf(value); diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampTypeTests.java b/src/test/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampTypeTests.java index 65884ac..9fcd64d 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampTypeTests.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/jdbc/types/TimestampTypeTests.java @@ -48,7 +48,11 @@ public class TimestampTypeTests { @CsvSource(value = { "2009-06-16T07:28:52.333, 1245137332333", "2015-01-01 00:34:46, 1420072486000", - "2015-01-01 00:34:46.778, 1420072486778" + "2015-01-01 00:34:46.778, 1420072486778", + "2015-01-01 00:34:46.778+00:00, 1420072486778", + "2015-01-01 00:34:46.778Z, 1420072486778", + "2015-01-01T00:34:46.778+01:00, 1420068886778", + "2015-01-01 00:34:46.778-02, 1420079686778", }) void testTimestampFromStringDefaultTZ(String stringValue, long longValue) { Timestamp timestamp = Assertions.assertDoesNotThrow(