How to use Joda-Time with java.sql.Timestamp

WolfmanDragon picture WolfmanDragon · Jul 2, 2009 · Viewed 62.4k times · Source

I Have a prepared statement

INSERT INTO mst(time) VALUES (?);

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.

Thanks.

Answer

Jack Leow picture Jack Leow · Jul 2, 2009

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