Monday, February 13, 2012

Time Zone conversions in Oracle SOA 10g BPEL process

Date/Time conversion from one timezone to another - I feel this is not a rare requirement for any global customer.

To simplify the post here i am giving

Problem Statement: How to change the date/time in the BPEL request to a date/time with different timezone?

Solution: Use Oracle SOA 10g BPEL's 'Java Embedded' activity and write java code for date conversion.

Though i am referring particularly Oracle SOA 10g here the idea can be used in any "JAVA" base SOA tool ( like JCAPA, Open ESB , etc...) if it allows a Java coding in their BPEL based interface development(OR in any form of interface development).

Here are the steps to follow

1.) Read input date/time value from the BPEL input message and convert it to java.lang.String Object

2.) Create two java.text.DateFormat objects one with the source timezone format (let us say the variable name is 'oSourceDtFormat') and second with target timezone format ( let us say the variable name is 'oTargetDtFormat' )

3.) Parse the date/time String to java.util.Date Object using oSourceDtFormat.parse("input date/time String") method

4.) Format the Date object in Step 3 in to the target date/time format, using oTargetDtFormat.format() method, it returns the required date/time in String format.


Here is the pseudo code. that can be used in Oracle SOA 10g 'Embedded Java' activity


String sInputTime=(String)getVariableData("sourceDateTime");
//Assuming the variable 'sourceDateTime' is holding input date/time value

java.util.TimeZone oTargetTZ = java.util.TimeZone.getTimeZone("CST");
//Assuming the target Time Zone is 'CST'
java.text.DateFormat oSourceDtFormat = new java.text.SimpleDateFormat("yyyyMMdd hh:mm a z");
//20100923 01:00 PM CDT assume the input date/time has timeZone component

java.text.DateFormat oTargetDtFormat = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
//target date/time format "yyyy/MM/dd HH:mm");

oTargetDtFormat.setTimeZone(oTargetTZ);
//Set the time zone to target time
java.util.Date oDate = null;

try{
oDate = (java.util.Date)oSourceDtFormat.parse(sInputTime);
try{
oDate = (java.util.Date)oSourceDtFormat.parse(sInputTime);
java.lang.String sSourceTime = oSourceDtFormat.format(oDate);
addAuditTrailEntry("Input bid-date-time is ::", sSourceTime);

java.lang.String sTargetTime = oTargetDtFormat.format(oDate);
setVariableData("targetDateTime", sTargetTime);
//Assuming the 'targetDateTime is a variable in BPEL process
addAuditTrailEntry("Date/Time in target timezone ::", sTargetTime);

}catch(Exception e){
addAuditTrailEntry(e);
}

Hope this helps..................