How to Convert Between Joda and Oracle's Date Classes

This is a very specific post to programming code. If you aren’t interested in this field, you can skip this one. I generally tag these posts with “code”, which is different from “coding”

Oracle’s Jdeveloper framework uses its own Date class implementation to make it easy to communicate with the Oracle Database DATE field. See oracle.jbo.domain.Date.

Anyone who’s worked for a reasonable time in the JEE/Java world knows that the no-brainer Date implementation library to use is Joda. Joda makes date comparisons, adding days, checking days, Daylight savings etc. all a breeze.

I’ve made a conversion chart to help with the inter-conversion between the different date formats because unfortunately there’s no straight forward way to go about it:

Diagram mapping joda and jbo dates

Converting from a jbo Date to Joda Date

oracle.jbo.domain.Date  jboDt = new oracle.jbo.domain.Date();   // the Jbo date that you want to convert

java.sql.Timestamp      sqlDtTm = jboDt.timestampValue();
org.joda.time.DateTime  jodaDt = new DateTime(sqlDtTm);

// Below would also work (But only date component captured and timestamp lost)
java.sql.Date           sqlDt = jboDt.dateValue();
org.joda.time.DateTime  jodaDt = new DateTime(sqlDt);

Sometimes, you also have to go the other way i.e from Joda to jbo (if you want to store the date field back into the database etc.)

Converting from a Joda Date to jbo Date

org.joda.time.DateTime  jodaDt  = new DateTime();   // the joda date that you want converted

java.util.Date          udt     = jodaDt.toDate();
java.sql.Date           sqlDt   = new java.sql.Date( udt.getTimestamp() ); // Timestamp ensures time component of date also captured
oracle.jbo.domain.Date  jboDt   = new oracle.jbo.domain.Date(sqlDt);