Is there a way to change the time zone of a date time value?

0 votes
asked Aug 7, 2013 by michael-h-5027 (13,630 points)
For example my message has a datetime value in EST and I want it in PST for the receiving system.

2 Answers

+1 vote
Best answer

If you know the timezone that the source date is in, then you will create a SimpleDateFormat for that timezone.  The output of the parse command will have a date in your local timezone.  You can then create a new SimpleDateFormat for the output timezone.

// starting date string
var dateString = '2017-03-22 09:01:15';
qie.debug('Starting Date: ' + dateString);

// specify the format of the string that you will be converting to a date
var estTimeFormat = new java.text.SimpleDateFormat('yyyy-MM-dd HH:mm:ss');

// specify the timezone that will be used for this conversion
// NOTE: You need to use the full timezone name, not the abbreviation (see for timezones)
qie.debug('New York TimeZone: ' + java.util.TimeZone.getTimeZone("America/New_York"));

// use the simple date format to parse the date.  The output date will be in your local timezone, so in this case the output is: 'Wed Mar 22 07:01:02 MDT 2017' for mountain daylight time
var myDate = estTimeFormat.parse(dateString);
qie.debug('Start date converted to local timezone (MDT): ' + myDate);

// NOTE: you can use the qie.formatDate('yyyy-MM-dd HH:mm:ss', myDate);
qie.debug('Formatted local time: ' + qie.formatDate('yyyy-MM-dd HH:mm:ss', myDate));

// now you can convert it to UTC
var utcTimeFormat = new java.text.SimpleDateFormat('yyyy-MM-dd HH:mm:ss');
qie.debug('UTC TimeZone: ' + java.util.TimeZone.getTimeZone("UTC"));

// this will format the output string using the UTC timezone
var utcString = utcTimeFormat.format(myDate);
qie.debug('UTC Date: ' + utcString);

answered Mar 22, 2017 by ben-s-7515 (11,100 points)
0 votes
You can create a custom mapping with something like the following:

// get a date in the current system timezone (ie. MDT)
var date = new java.util.Date(qie.deduceDate(qie.getSystemDate()).getTime());

// create a SimpleDateFormat object (defaults to current system timezone)
var utc_format = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'");

// explicitly change the SimpleDateFormat object to UTC timezone

// format the current system date to UTC
messageCache.setValue('securityCreatedTimestamp', utc_format.format(date));

// OR...
messageCache.setValue('securityCreatedTimestamp', qie.evaluateTemplate("{UTC_DATE[yyyy-MM-dd'T'HH:mm:ss.sss'Z']}"));

answered Aug 7, 2013 by michael-h-5027 (13,630 points)
edited Sep 21, 2018 by michael-h-5027
commented Sep 26, 2013 by oscar-p-6691 (550 points)
edited Sep 26, 2013 by oscar-p-6691
You can also subtract an hour or add an hour.  I used some of the above code and changed it to the format I needed.  Thanks for the above example.  It really helped me get the below working :)

// get a date in the current timezone
var date = new java.util.Date(qie.deduceDate(source.getNode("OBR-6")).getTime());
// convert the above date to new Date(won't work without doing this)
var olddate = new Date(date);
// subtract 1 hour
var newdate = new Date(olddate.getTime() - (1*60*60*1000));
// create a SimpleDateFormat object
var date_format = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
// insert new date to wherever you want
message.setNode('OBR-6', date_format.format(newdate));