如何将 Joda-Time 与 java.sql.Timestamp 一起使用

2022-01-13 00:00:00 postgresql timestamp java jodatime

我有一个准备好的声明

INSERT INTO mst(time) VALUES (?);

其中时间的类型是 时间戳 在 PostgreSQL 数据库.
我正在插入 Joda-Time DateTime 对象,或者我应该说我正在尝试.我找不到将 DateTime 对象转换为 java 的方法.sql.时间戳.我已阅读 Joda-Time 文档,但没有看到对此的参考.

where time is of type Timestamp in a PostgreSQL database.
I am inserting a Joda-Time DateTime object, or I should say I am trying to. I can find no way to convert the DateTime object into a java.sql.Timestamp. I have read the Joda-Time docs and see no reference to this.

谢谢.

推荐答案

您可以先将 Joda DateTime 转换为 long(自纪元以来的毫秒),然后从中创建时间戳.

You can convert a Joda DateTime to a long (millis since the epoch) first, and then create a Timestamp from that.

DateTime dateTime = new DateTime();
Timestamp timeStamp = new Timestamp(dateTime.getMillis());

相关文章