diff --git a/history/latest/3127.md b/history/latest/3127.md
new file mode 100644
index 000000000..ed7997157
--- /dev/null
+++ b/history/latest/3127.md
@@ -0,0 +1,4 @@
+- **[jdbc-v2]** A `LocalDateTime` bound via `setObject` on the text path is now interpreted in the
+ server/column time zone instead of the JVM default time zone, matching `Timestamp` and the reader.
+ Behaviour change on a non-UTC JVM; set `jdbc_legacy_localdatetime_jvm_timezone=true` to restore the
+ old behaviour. (https://github.com/ClickHouse/clickhouse-java/issues/3127)
diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/DriverProperties.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/DriverProperties.java
index 8bbcfd10b..b6abddd46 100644
--- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/DriverProperties.java
+++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/DriverProperties.java
@@ -72,6 +72,12 @@ public enum DriverProperties {
+ "max_result_rows with result_overflow_mode=break. This overrides the overflow mode that the user sets.")
USE_MAX_RESULT_ROWS("jdbc_use_max_result_rows", String.valueOf(Boolean.FALSE)),
+ @ConfigDescription("Legacy. When true, a LocalDateTime bound through setObject on the text path is "
+ + "converted using the JVM default time zone (the behaviour before the fix for issue #3127). "
+ + "The default false sends the local date-time unchanged, so the server interprets it in the "
+ + "column or server time zone, matching Timestamp and the reader.")
+ LEGACY_LOCALDATETIME_JVM_TIMEZONE("jdbc_legacy_localdatetime_jvm_timezone", String.valueOf(Boolean.FALSE)),
+
/**
* Configures what SQL parser will be used. Choices:
*
diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java
index c2487d289..8fa1ad8ba 100644
--- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java
+++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java
@@ -780,7 +780,12 @@ private String encodeObject(Object x, Long length) throws SQLException {
} else if (x instanceof Timestamp) {
return QUOTE + DataTypeUtils.DATE_TIME_WITH_OPTIONAL_NANOS.format(((Timestamp) x).toLocalDateTime()) + QUOTE;
} else if (x instanceof LocalDateTime) {
- return "fromUnixTimestamp64Nano(" + DataTypeUtils.toUnixTimestampString((LocalDateTime) x, defaultCalendar.getTimeZone()) + ")";
+ if (connection.getJdbcConfig().isFlagSet(DriverProperties.LEGACY_LOCALDATETIME_JVM_TIMEZONE)) {
+ // Legacy (pre-#3127): bake the JVM default time zone into the stored instant.
+ return "fromUnixTimestamp64Nano(" + DataTypeUtils.toUnixTimestampString((LocalDateTime) x, defaultCalendar.getTimeZone()) + ")";
+ }
+ // Send the local date-time unchanged; the server interprets it in the column/server time zone (#3127).
+ return QUOTE + DataTypeUtils.DATE_TIME_WITH_OPTIONAL_NANOS.format((LocalDateTime) x) + QUOTE;
} else if (x instanceof OffsetDateTime) {
return encodeObject(((OffsetDateTime) x).toInstant());
} else if (x instanceof ZonedDateTime) {
diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCDateTimeTests.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCDateTimeTests.java
index 10094ad63..696a660c8 100644
--- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCDateTimeTests.java
+++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/JDBCDateTimeTests.java
@@ -313,4 +313,34 @@ void testTimestampInRange() throws Exception {
}
}
}
+
+ // Regression for #3127: a LocalDateTime via setObject on the text path must be interpreted in
+ // the server time zone (like a string literal and Timestamp), not the JVM default time zone.
+ @Test(groups = {"integration"})
+ void testSetObjectLocalDateTimeTextPathServerTimeZone() throws Exception {
+ LocalDateTime ldt = LocalDateTime.parse("2024-01-15T12:00:00");
+ try (Connection conn = getJdbcConnection();
+ Statement stmt = conn.createStatement()) {
+ stmt.executeUpdate("DROP TABLE IF EXISTS test_ldt_tz");
+ stmt.executeUpdate("CREATE TABLE test_ldt_tz (kind String, t DateTime, t64 DateTime64(3)) ENGINE = MergeTree ORDER BY kind");
+ try (PreparedStatement ps = conn.prepareStatement("INSERT INTO test_ldt_tz VALUES (?, ?, ?)")) {
+ ps.setString(1, "ldt");
+ ps.setObject(2, ldt);
+ ps.setObject(3, ldt);
+ ps.addBatch();
+ ps.executeBatch();
+ }
+ // The same wall clock as a string literal: the server always interprets it in its own zone.
+ stmt.executeUpdate("INSERT INTO test_ldt_tz VALUES ('str', '2024-01-15 12:00:00', '2024-01-15 12:00:00')");
+ try (ResultSet rs = stmt.executeQuery("SELECT " +
+ "anyIf(toString(t), kind = 'ldt') AS lt, anyIf(toString(t), kind = 'str') AS st, " +
+ "anyIf(toString(t64), kind = 'ldt') AS lt64, anyIf(toString(t64), kind = 'str') AS st64 " +
+ "FROM test_ldt_tz")) {
+ Assert.assertTrue(rs.next());
+ Assert.assertEquals(rs.getString("lt"), rs.getString("st"));
+ Assert.assertEquals(rs.getString("lt64"), rs.getString("st64"));
+ }
+ stmt.executeUpdate("DROP TABLE IF EXISTS test_ldt_tz");
+ }
+ }
}