Created
November 8, 2022 08:36
-
-
Save raphw/b1cae6aaeb8a6f7b65a7aa7603bcd426 to your computer and use it in GitHub Desktop.
Timestamps with JDBC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.sql.Timestamp; | |
import java.time.LocalDateTime; | |
import java.time.ZoneOffset; | |
import java.time.ZonedDateTime; | |
import java.util.Calendar; | |
import java.util.TimeZone; | |
import com.zaxxer.hikari.HikariConfig; | |
import com.zaxxer.hikari.HikariDataSource; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.testcontainers.containers.JdbcDatabaseContainer; | |
import org.testcontainers.containers.PostgreSQLContainer; | |
public class TimestampTest { | |
@Rule | |
public JdbcDatabaseContainer<?> container = new PostgreSQLContainer<>("postgres:11"); | |
private HikariDataSource dataSource; | |
@Before | |
public void setUp() { | |
HikariConfig config = new HikariConfig(); | |
config.setJdbcUrl(container.getJdbcUrl()); | |
config.setUsername(container.getUsername()); | |
config.setPassword(container.getPassword()); | |
dataSource = new HikariDataSource(config); | |
} | |
@After | |
public void tearDown() { | |
dataSource.close(); | |
} | |
@Test | |
public void test_abc() throws Exception { | |
Calendar utc = Calendar.getInstance(TimeZone.getTimeZone("UTC")); | |
LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC); | |
System.out.println("Now: " + ZonedDateTime.now()); | |
System.out.println("Now in UTC: " + LocalDateTime.now()); | |
System.out.println("UTC: " + LocalDateTime.now(ZoneOffset.UTC)); | |
System.out.println("JVM - " + TimeZone.getDefault().getID() + " - hours: " + now.getHour()); | |
try (Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement()) { | |
//stmt.execute("SET TIME ZONE 'UTC'"); | |
stmt.execute("CREATE TABLE MY_TEST (C1 TIMESTAMP WITH TIME ZONE, " + | |
"C2 TIMESTAMP WITHOUT TIME ZONE, " + | |
"C3 TIMESTAMP WITH TIME ZONE, " + | |
"C4 TIMESTAMP WITHOUT TIME ZONE, " + | |
"C5 TIMESTAMP WITH TIME ZONE, " + | |
"C6 TIMESTAMP WITHOUT TIME ZONE, " + | |
"C7 TIMESTAMP WITH TIME ZONE, " + | |
"C8 TIMESTAMP WITHOUT TIME ZONE)"); | |
try (ResultSet rs = stmt.executeQuery("SELECT current_setting('TIMEZONE');")) { | |
while (rs.next()) { | |
System.out.println("Database: " + rs.getString(1)); | |
} | |
} | |
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO MY_TEST " + | |
"(C1, C2, C3, C4, C5, C6, C7, C8) VALUES " + | |
"(?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP AT TIME ZONE 'UTC', CURRENT_TIMESTAMP AT TIME ZONE 'UTC')")) { | |
ps.setTimestamp(1, Timestamp.valueOf(now)); | |
ps.setTimestamp(2, Timestamp.valueOf(now)); | |
ps.setTimestamp(3, Timestamp.valueOf(now), utc); | |
ps.setTimestamp(4, Timestamp.valueOf(now), utc); | |
ps.executeUpdate(); | |
} | |
try (ResultSet rs = stmt.executeQuery("SELECT * FROM MY_TEST")) { | |
while (rs.next()) { | |
printResult(rs, utc, "C1", now.getHour(), "setTimestamp(LocalDateTime) -> with TZ"); | |
printResult(rs, utc, "C2", now.getHour(), "setTimestamp(LocalDateTime, UTC) -> with TZ"); | |
printResult(rs, utc, "C3", now.getHour(), "setTimestamp(LocalDateTime, UTC) -> with TZ"); | |
printResult(rs, utc, "C4", now.getHour(), "setTimestamp(LocalDateTime, UTC> -> without TZ"); | |
printResult(rs, utc, "C5", now.getHour(), "CURRENT_TIMESTAMP -> with TZ"); | |
printResult(rs, utc, "C6", now.getHour(), "CURRENT_TIMESTAMP -> without TZ"); | |
printResult(rs, utc, "C7", now.getHour(), "CURRENT_TIMESTAMP @ UTC -> with TZ"); | |
printResult(rs, utc, "C8", now.getHour(), "CURRENT_TIMESTAMP @ UTC -> without TZ"); | |
} | |
} | |
} | |
} | |
private static void printResult(ResultSet rs, Calendar utc, String column, int expected, String label) throws SQLException { | |
System.out.println("------- " + column + " - " + label); | |
System.out.println(rs.getString(column)); | |
System.out.println("getTimestamp(): " + (rs.getTimestamp(column).toLocalDateTime().getHour() - expected)); | |
System.out.println("getTimestamp(UTC): " + (rs.getTimestamp(column, utc).toLocalDateTime().getHour() - expected)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment