Friday, July 26, 2013

How to convert date into oracle sysdate using java or how to send date to Oracle procedure



This may be something of your use, hence sharing.

This is how we can send a value to a DATE field in procedure, which will match DB sysdate. 

Please note that it is assumed here, the DB takes the current date in “sysdate” as GMT/UTC timestamp. 
Whereas the JVM may reside on IST(in case of local machine), PST or probably some other timestamp. (This was the actual case in one of my recent projects)

Hence, to maintain a consistency between Oracle’s sysdate and the date which is passed to an Oracle Proc/Function, the below code can come very handy.

stmt.setTimestamp(12, new java.sql.Timestamp(System.currentTimeMillis()), Calendar.getInstance(TimeZone.getTimeZone("GMT")));


Here, 12 is the position of the IN parameter, which is defined as a DATE field in the Backend pl/sql procedure.

These can be used in “last_updated_time” or "creation_date" kind of fields.

PS: I know, I could have used sysdate directly in the procedure too! But, this is a Java way out.

No comments:

Post a Comment

Prototype

Prototype is another creation pattern. Intent:  - Intent is to create objects by cloning existing instance  - Specify the kinds of obj...