java simple date pattern TO oracle sql date pattern

I have following web application: Users can enter java simple date format patterns and a date (of course matching to the java simple date format pattern) and I want to store these date in an oracle database.

Therefore I need to translate the java simple date format pattern into the oracle pattern. E.g:

  • "dd-MM-yyyy HH:mm:ss" into "DD-MM-YYYY HH24:MI:SS"
  • "dd-MM-yy HH:mm:ss" into "DD-MM-YY HH24:MI:SS"
  • "dd-MM-yy HH:mm" into "DD-MM-YY HH24:MI"

and so on.

Instead of the following code just having one SimpleDateFormat I would like to have all or at least a big bunch of SimpleDateFormatPatterns translated into Oracle pattern:

SimpleDateFormat sFormat = new SimpleDateFormat( "dd-MM-yyyy HH:mm:ss");
String sqlSnippet = "TO_DATE('" + sFormat.format(date) + "','DD-MM-YYYY HH24:MI:SS')";

Is there a library or maybe just a mapping list to do this?

Thanks.

Edit: I need to build the SQL by hand as the user defines the criteria, compare operators and joins in the user interface. In the end I have something like this

AND col2 > TO_DATE('26-09-2012','DD-MM-YYYY')
Jon Skeet
people
quotationmark

Therefore I need to translate the java simple date format pattern into the oracle pattern

No, you don't. You should instead use a PreparedStatement, and call setDate or setTimestamp on it to specify the value you're interested in.

Avoid string conversions unless they're fundamentally part of what you're trying to do (e.g. displaying a date/time in a UI). For simply transferring information from your app to your database or vice versa, you should reduce the number of conversions required as far as possible.

people

See more on this question at Stackoverflow