Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 273
WolfmanDragon Avatar asked Jul 01 '09 23:07

WolfmanDragon


People also ask

Does Java SQL timestamp have a timezone?

getTimestamp call has no Calendar parameter, or the Calendar parameter value is null, the IBM Data Server Driver for JDBC and SQLJ uses the default time zone when it constructs the returned object.

Is Joda-time followed in Java 8?

Joda-Time is an API created by joda.org which offers better classes and having efficient methods to handle date and time than classes from java. util package like Calendar, Gregorian Calendar, Date, etc. This API is included in Java 8.0 with the java.


1 Answers

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()); 
like image 54
Jack Leow Avatar answered Sep 24 '22 13:09

Jack Leow