diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java index b017691bf..acf6f71d6 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerResultSet.java @@ -2373,6 +2373,24 @@ public T getObject(int columnIndex, Class type) throws SQLException { returnValue = getTime(columnIndex); } else if (type == java.sql.Timestamp.class) { returnValue = getTimestamp(columnIndex); + } else if (type == java.time.LocalDateTime.class + || type == java.time.LocalDate.class + || type == java.time.LocalTime.class) { + java.sql.Timestamp ts = getTimestamp(columnIndex, + Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"))); + if (ts == null) { + returnValue = null; + } else { + java.time.LocalDateTime ldt = java.time.LocalDateTime + .ofInstant(ts.toInstant(), java.time.ZoneId.of("UTC")); + if (type == java.time.LocalDateTime.class) { + returnValue = ldt; + } else if (type == java.time.LocalDate.class) { + returnValue = ldt.toLocalDate(); + } else { + returnValue = ldt.toLocalTime(); + } + } } else if (type == microsoft.sql.DateTimeOffset.class) { returnValue = getDateTimeOffset(columnIndex); } else if (type == UUID.class) { diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java index 8add5c13c..eb73fd3ff 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java @@ -21,6 +21,10 @@ import java.sql.SQLException; import java.sql.SQLXML; import java.sql.Statement; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.TimeZone; import java.util.UUID; import org.junit.jupiter.api.Test; @@ -240,6 +244,48 @@ public void testJdbc41ResultSetMethods() throws SQLException { } } + /** + * Tests getObject(n, java.time.LocalDateTime.class). + * + * @throws SQLException + */ + @Test + public void testGetObjectAsLocalDateTime() throws SQLException { + try (Connection con = DriverManager.getConnection(connectionString); Statement stmt = con.createStatement()) { + TimeZone prevTimeZone = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("America/Edmonton")); + + // a local date/time that does not actually exist because of Daylight Saving Time + final String testValueDate = "2018-03-11"; + final String testValueTime = "02:00:00.1234567"; + final String testValueDateTime = testValueDate + "T" + testValueTime; + + stmt.executeUpdate( + "CREATE TABLE " + tableName + " (id INT PRIMARY KEY, dt2 DATETIME2)"); + stmt.executeUpdate( + "INSERT INTO " + tableName + " (id, dt2) VALUES (1, '" + testValueDateTime + "')"); + + try (ResultSet rs = stmt.executeQuery("SELECT dt2 FROM " + tableName + " WHERE id=1")) { + rs.next(); + + LocalDateTime expectedLocalDateTime = LocalDateTime.parse(testValueDateTime); + LocalDateTime actualLocalDateTime = rs.getObject(1, LocalDateTime.class); + assertEquals(expectedLocalDateTime, actualLocalDateTime); + + LocalDate expectedLocalDate = LocalDate.parse(testValueDate); + LocalDate actualLocalDate = rs.getObject(1, LocalDate.class); + assertEquals(expectedLocalDate, actualLocalDate); + + LocalTime expectedLocalTime = LocalTime.parse(testValueTime); + LocalTime actualLocalTime = rs.getObject(1, LocalTime.class); + assertEquals(expectedLocalTime, actualLocalTime); + } finally { + Utils.dropTableIfExists(tableName, stmt); + TimeZone.setDefault(prevTimeZone); + } + } + } + /** * Tests ResultSet#isWrapperFor and ResultSet#unwrap. *