Convert joda.time.DateTime to java.sql.Date and retain time zone

Viewed 27367

I have the following scenario:

  • Swing control that returns a Calendar object
  • Intermediate DateTime object that I use to do heavy date/time manipulation (joda)
  • Database connection (OraclePreparedStatement) that only takes a java.sql.Date object

My problem is that the Calendar and DateTime objects are properly displaying the date in GMT (which I want), but when I convert to java.sql.Date in order to send to the database, the date is converted to the local time zone.

For example:

  • Calendar and DateTime are 2012-08-13T23:59:59.000Z (correct GMT)
  • Resulting java.sql.Date is 2012-08-14 (incorrect local UTC+2 date)

Below is the code I'm using to do the conversion.

DateTime dateGmt = new DateTime(calendarGmt.getTimeInMillis(), DateTimeZone.UTC);
java.sql.Date sqlDate = new java.sql.Date(dateGmt.getMillis());

I don't know how to create a java.sql.Date object while retaining the correct time zone. It's also entirely possible that I'm doing an incorrect conversion.

3 Answers
Related