Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```
- create table entries(
- timestamp timestamp without time zone
- );
- insert into entries values ('2018-07-11 13:33:09');
- ```
- Using the following scala code:
- ```
- import java.sql.DriverManager
- import java.time.{ZoneId, ZonedDateTime}
- object jdbctest extends App {
- // Ensure your timezone is not UTC
- val url = "jdbc:postgresql://localhost/ittest?user=postgres"
- val conn = DriverManager.getConnection(url)
- val instant = ZonedDateTime.of(2018, 7, 11, 13, 33, 9, 0, ZoneId.of("UTC")).toInstant
- println(instant)
- val stmt = conn.prepareStatement("select count(1) from entries where timestamp = ?")
- stmt.setTimestamp(1, java.sql.Timestamp.from(instant))
- val rs = stmt.executeQuery()
- if (rs.next())
- println(s"Count with converted instant: ${rs.getInt(1)}")
- stmt.setTimestamp(1, java.sql.Timestamp.valueOf("2018-07-11 13:33:09"))
- val rs2 = stmt.executeQuery()
- if (rs2.next())
- println(s"Count with timestamp.valueOf: ${rs2.getInt(1)}")
- }
- ``
- You get:
- ```
- 2018-07-11T13:33:09Z
- Count with converted instant: 0
- Count with timestamp.valueOf: 1
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement