Advertisement
Guest User

Untitled

a guest
Sep 5th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. ```
  2.  
  3. create table entries(
  4. timestamp timestamp without time zone
  5. );
  6.  
  7. insert into entries values ('2018-07-11 13:33:09');
  8. ```
  9.  
  10. Using the following scala code:
  11.  
  12. ```
  13.  
  14. import java.sql.DriverManager
  15. import java.time.{ZoneId, ZonedDateTime}
  16.  
  17. object jdbctest extends App {
  18. // Ensure your timezone is not UTC
  19.  
  20. val url = "jdbc:postgresql://localhost/ittest?user=postgres"
  21. val conn = DriverManager.getConnection(url)
  22.  
  23. val instant = ZonedDateTime.of(2018, 7, 11, 13, 33, 9, 0, ZoneId.of("UTC")).toInstant
  24. println(instant)
  25.  
  26. val stmt = conn.prepareStatement("select count(1) from entries where timestamp = ?")
  27. stmt.setTimestamp(1, java.sql.Timestamp.from(instant))
  28.  
  29. val rs = stmt.executeQuery()
  30. if (rs.next())
  31. println(s"Count with converted instant: ${rs.getInt(1)}")
  32.  
  33. stmt.setTimestamp(1, java.sql.Timestamp.valueOf("2018-07-11 13:33:09"))
  34.  
  35. val rs2 = stmt.executeQuery()
  36. if (rs2.next())
  37. println(s"Count with timestamp.valueOf: ${rs2.getInt(1)}")
  38.  
  39. }
  40. ``
  41.  
  42. You get:
  43.  
  44. ```
  45. 2018-07-11T13:33:09Z
  46. Count with converted instant: 0
  47. Count with timestamp.valueOf: 1
  48. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement