How can i insert timestamp with timezone in postgresql with prepared statement?

后端 未结 2 1417
北荒
北荒 2021-02-18 23:48

I am trying to insert to a timestamp with timezone field of my DB a string which includes date, time and timezone using prepared statement.

The problem is that Timestamp

相关标签:
2条回答
  • 2021-02-19 00:28

    I believe that you could use one more field in your database, which would include the time zone. And calculate the time manually after you get these two fields

    0 讨论(0)
  • 2021-02-19 00:41

    The basic problem is that a java.sql.Timestamp does not contain timezone information. I think it is always assumed to be "local timezone".

    On solution I can think of is to not use a parameter in a PreparedStatement, but a timezone literal in SQL:

    update foo
      set ts_col = timestamp with time zone '2012-08-24 14:00:00 +02:00'`;
    

    Another possible solution could be to pass a properly formatted String to a PrepareStatement that uses to_timestamp():

    String sql = "update foo set ts_col = to_timestamp(?, 'yyyy-mm-dd hh24:mi:ss')"; 
    PreparedStatement pstmt = connection.prepareStatement(sql);
    pstmt.setString(1, "2012-08-24 14:00:00 +02:00");
    
    0 讨论(0)
提交回复
热议问题