feat(java): implement getDate() in JDBC4ResultSet

This commit is contained in:
kimminseok
2025-10-07 14:22:13 +09:00
parent fb370c63a6
commit 6adc272d75
2 changed files with 71 additions and 11 deletions

View File

@@ -812,15 +812,24 @@ public final class JDBC4ResultSet implements ResultSet, ResultSetMetaData {
@Override
@Nullable
public Date getDate(int columnIndex, Calendar cal) throws SQLException {
// TODO: Properly handle timezone conversion with Calendar
return getDate(columnIndex);
final Date date = getDate(columnIndex);
if (date == null || cal == null) {
return date;
}
final Calendar localCal = Calendar.getInstance();
localCal.setTime(date);
final long offset = cal.getTimeZone().getOffset(date.getTime()) -
localCal.getTimeZone().getOffset(date.getTime());
return new Date(date.getTime() + offset);
}
@Override
@Nullable
public Date getDate(String columnLabel, Calendar cal) throws SQLException {
// TODO: Properly handle timezone conversion with Calendar
return getDate(columnLabel);
return getDate(findColumn(columnLabel), cal);
}
@Override

View File

@@ -1,18 +1,14 @@
package tech.turso.jdbc4;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.Reader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.ByteBuffer;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
@@ -21,6 +17,8 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import tech.turso.TestUtils;
import static org.junit.jupiter.api.Assertions.*;
class JDBC4ResultSetTest {
private Statement stmt;
@@ -715,4 +713,57 @@ class JDBC4ResultSetTest {
resultSet.next();
assertEquals(3, resultSet.getRow());
}
@Test
void test_getDate_with_calendar() throws Exception {
stmt.executeUpdate("CREATE TABLE test_date_cal (date_col BLOB);");
// 2025-10-07 03:00:00 UTC in milliseconds
long utcTime = 1728270000000L;
byte[] timeBytes = ByteBuffer.allocate(Long.BYTES).putLong(utcTime).array();
StringBuilder hexString = new StringBuilder();
for (byte b : timeBytes) {
hexString.append(String.format("%02X", b));
}
stmt.executeUpdate("INSERT INTO test_date_cal (date_col) VALUES (X'" + hexString + "');");
ResultSet resultSet = stmt.executeQuery("SELECT * FROM test_date_cal");
assertTrue(resultSet.next());
// Get date with UTC calendar
Calendar utcCal = Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"));
Date utcDate = resultSet.getDate(1, utcCal);
// Get date with Seoul calendar (UTC+9)
Calendar seoulCal = Calendar.getInstance(java.util.TimeZone.getTimeZone("Asia/Seoul"));
Date seoulDate = resultSet.getDate(1, seoulCal);
// Seoul time should be 9 hours ahead
long timeDiff = seoulDate.getTime() - utcDate.getTime();
assertEquals(9 * 60 * 60 * 1000, timeDiff);
}
@Test
void test_getDate_with_calendar_columnLabel() throws Exception {
stmt.executeUpdate("CREATE TABLE test_date_cal (created_at BLOB);");
// 2025-10-07 03:00:00 UTC in milliseconds
long utcTime = 1728270000000L;
byte[] timeBytes = ByteBuffer.allocate(Long.BYTES).putLong(utcTime).array();
StringBuilder hexString = new StringBuilder();
for (byte b : timeBytes) {
hexString.append(String.format("%02X", b));
}
stmt.executeUpdate("INSERT INTO test_date_cal (created_at) VALUES (X'" + hexString + "');");
ResultSet resultSet = stmt.executeQuery("SELECT * FROM test_date_cal");
assertTrue(resultSet.next());
Calendar utcCal = Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"));
Date date = resultSet.getDate("created_at", utcCal);
assertNotNull(date);
}
}