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

How to use Joda-Time with java.sql.Timestamp(如何将 Joda-Time 与 java.sql.Timestamp 一起使用)

本文介绍了如何将 Joda-Time 与 java.sql.Timestamp 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个准备好的声明

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());

这篇关于如何将 Joda-Time 与 java.sql.Timestamp 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将 Joda-Time 与 java.sql.Timestamp 一起使用

基础教程推荐