I have tests that use a H2 db, and the data set is populated with timestamps relative to the current timestamp, using expressions such as DATEADD(DAY, -3, CURRENT_TIMESTAMP).
Let's say current timestamp is 2023-03-27 18:00:00
, and assertion expects a query to return a timestamp at same time of day 3 days ago, then the (computed) expected timestamp in Java would correctly be 2023-03-24 18:00:00
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
try (Connection c = DriverManager.getConnection("jdbc:h2:mem:1")) {
Statement s = c.createStatement();
s.execute("SET TIME ZONE 'Europe/Paris'");
ResultSet rs = s.executeQuery("VALUES DATEADD(DAY, -3, TIMESTAMP WITH TIME ZONE '2023-03-27 18:00:00+02')");
rs.next();
System.out.println(rs.getObject(1)); // Returns OffsetDateTime
System.out.println(rs.getObject(1, LocalDateTime.class)); // Implicit conversion to TIMESTAMP data type
System.out.println(rs.getTimestamp(1)); // Legacy method, shouldn't be used in modern applications, especially for TIMESTAMP WITH TIME ZONE data type
}
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
try (Connection c = DriverManager.getConnection("jdbc:h2:mem:1")) {
Statement s = c.createStatement();
s.execute("SET TIME ZONE 'Europe/Paris'");
ResultSet rs = s.executeQuery("VALUES DATEADD(DAY, -3, TIMESTAMP '2023-03-27 18:00:00')");
rs.next();
System.out.println(rs.getObject(1, LocalDateTime.class));
System.out.println(rs.getTimestamp(1)); // Legacy method, shouldn't be used in modern applications
}