cancel
Showing results for 
Search instead for 
Did you mean: 

Convert xsd:dateTime to UTC

wolfgang_hegmann
Explorer
0 Kudos

Hello community,

Within a message mapping I have to convert a field containing date, time and timezone to UTC/GMT timezone.

Example:

2008-10-06T01:11:06+02:00 = Incoming value from source field, incl. local time + time zone offset

2008-10-05T23:11:06 = Expected value of target field, incl. UTC date and time

How can I achieve this conversion with an UDF?

Best regards,

Wolfgang

Accepted Solutions (1)

Accepted Solutions (1)

former_member518917
Participant
0 Kudos

Hi,

U can chabge the time zone of the calender ussing TimeZone java class .

Try this UDF:

Import : java.text.*;

UDF:

AbstractTrace trace = container.getTrace();

String strDate =a;// "2008-10-06T01:11:06+0200";

Date date=null;

SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

try{

String dt = strDate;

date = sdfSource.parse(dt);

trace.addInfo("NOW1:"+date);

//Time Zone

Calendar cal1 = new GregorianCalendar(TimeZone.getTimeZone("GMT+05:30"));

trace.addInfo("Current Timezone="+cal1.getTimeZone().getDisplayName());

String timeZoneId = "UTC";//Canada/Central"; // EX.

trace.addInfo("Getting Time in the timezone="+timeZoneId);

Calendar mbCal = new GregorianCalendar(TimeZone.getTimeZone(timeZoneId));

mbCal.setTimeInMillis(date.getTime());

Calendar cal = Calendar.getInstance();

cal.set(Calendar.YEAR, mbCal.get(Calendar.YEAR));

cal.set(Calendar.MONTH, mbCal.get(Calendar.MONTH));

cal.set(Calendar.DAY_OF_MONTH, mbCal.get(Calendar.DAY_OF_MONTH));

cal.set(Calendar.HOUR_OF_DAY, mbCal.get(Calendar.HOUR_OF_DAY));

cal.set(Calendar.MINUTE, mbCal.get(Calendar.MINUTE));

cal.set(Calendar.SECOND, mbCal.get(Calendar.SECOND));

cal.set(Calendar.MILLISECOND, mbCal.get(Calendar.MILLISECOND));

Date newdt = cal.getTime();

trace.addInfo("Getting Time in the timezone=####"+newdt);

SimpleDateFormat sdfDestination = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

strDate = sdfDestination.format(newdt);

trace.addInfo("NOW3:"+strDate);

}catch(ParseException pe){

trace.addInfo("Parse Exception : " + pe);

}

return strDate;

Regards

Ritu

Answers (0)