Java 拆下';T';从日期时间转换为sql日期
我将json字段设置为Java 拆下';T';从日期时间转换为sql日期,java,postgresql,datetime,Java,Postgresql,Datetime,我将json字段设置为 {"dateTime" : "2018-06-02T15:49:23"} 现在我需要在postgres存储过程中使用这个字段作为输入参数。在SP中,输入字段类型为timestamp,因为我需要在sql timestamp中转换此dateTime,中间不带“T”(仅2018-06-02 15:49:23)。 非常感谢您对如何在Java中实现它的任何建议。您可以选择它: t=# select ('{"dateTime" : "2018-06-02T15:49:23"}'::
{"dateTime" : "2018-06-02T15:49:23"}
现在我需要在postgres存储过程中使用这个字段作为输入参数。在SP中,输入字段类型为timestamp,因为我需要在sql timestamp中转换此dateTime,中间不带“T”(仅2018-06-02 15:49:23)。
非常感谢您对如何在Java中实现它的任何建议。您可以选择它:
t=# select ('{"dateTime" : "2018-06-02T15:49:23"}'::json->>'dateTime')::timestamp;
timestamp
---------------------
2018-06-02 15:49:23
(1 row)
我建议您在Java中将字符串解析为
LocalDateTime
,而不仅仅是删除T
。one argLocalDateTime.parse
方法执行以下操作:
LocalDateTime dateTime = LocalDateTime.parse("2018-06-02T15:49:23");
然后,您可以将LocalDateTime
传递给存储过程,它需要一个时间戳
CallableStatement cs = yourConnection.prepareCall("{call your_stored_procedure(?)}");
cs.setObject(1, dateTime);
在过去,我们会在这里使用
java.sql.Timestamp
。那门课已经过时很久了。对于Java 8和JDBC 4.2或更高版本,我们更喜欢使用LocalDateTime
或Instant
select(“{”dateTime:“2018-06-02T15:49:23”}”:json->“dateTime”)::timestamp
-您可以选择它,无需在Java中解析,而只需删除T
我建议您在Java中将字符串解析为LocalDateTime
(一个参数LocalDateTime.parse
方法就是这样做的),并将LocalDateTime
传递给您的存储过程。@Ole yes。!LocalDateTime在这里为我工作。使用Joda时间?
CallableStatement cs = yourConnection.prepareCall("{call your_stored_procedure(?)}");
cs.setObject(1, dateTime);