You need to sign in to do that
Don't have an account?

How to convert Datetime from GMT/TimeZone to another TimeZone
Couldn't find any doc /api reference that explicitly describes how to convert a given GMT Datetime to a desired TimeZone Datetime. All of the docs talk about GMT and "Local" TimeZone, unless I've missed something obvious.
I have start and end dates in the database, which are in GMT TimeZone. I need to convert them to a different TimeZone (BusinessHours TimeZone) so that I can use "BusinessHours.diff" API to determine elapsed time.
Thanks
Hrmm. How about:
Case thisCase = [select CreatedDate from
Case limit 1]; // gets the GMT date from database on a case
Datetime thisDT = thisCase.CreatedDate;
String myDate = thisDT.format('yyyy-MM-dd HH:mm:ss', 'PST'); // using object.format maybe? Example for pacific standard time.
Outputs:
thisDT- 2009-10-30 23:03:48 (GMT)
myDate- 2009-10-30 16:03:48 (PST).
I guess you could try to convert the myDate string back to a datetime object (but valueOf kept converting the time back to GMT):
Datetime convertedDate = datetime.valueOf(myDate);
Apex Datetime methods:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm
Java simpledateformat (great resource):
http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html#year
Thanks. Apologies for not responding earlier.
I had construction the new Datetime by extracting the components from fromatted string.
My final solution:
Convert both dates to BusinessHours TimeZone and use BusinessHours.diff method to determine the duration in mills.
BusinessHours corpBH = [select Id, TimeZoneSidKey from BusinessHours where Name = 'Corporate'];
Integer corpYear = Integer.valueOf(crtDateStr.substring(0, 4));
Integer corpMonth = Integer.valueOf(crtDateStr.substring(5, 7));
Integer corpDay = Integer.valueOf(crtDateStr.substring(8, 10));
Integer corpHours = Integer.valueOf(crtDateStr.substring(12, 13));
Integer corpMins = Integer.valueOf(crtDateStr.substring(14, 16));
Integer corpSecs = Integer.valueOf(crtDateStr.substring(17, 19));
DateTime corpBeginDate = DateTime.newInstance(corpYear, corpMonth, corpDay, corpHours, corpMins, corpSecs);
String modDateStr = modDatetime.format('yyyy-MM-dd HH:mm:ss', corpBH.TimeZoneSidKey);
corpYear = Integer.valueOf(modDateStr.substring(0, 4));
corpMonth = Integer.valueOf(modDateStr.substring(5, 7));
corpDay = Integer.valueOf(modDateStr.substring(8, 10));
corpHours = Integer.valueOf(modDateStr.substring(12, 13));
corpMins = Integer.valueOf(modDateStr.substring(14, 16));
corpSecs = Integer.valueOf(modDateStr.substring(17, 19));
DateTime corpEndDate = DateTime.newInstance(corpYear, corpMonth, corpDay, corpHours, corpMins, corpSecs);
Long diff = BusinessHours.diff(corpBH.Id, corpBeginDate, corpEndDate);
Thanks.
String TimeZones = '(GMT+05:30) India Standard Time (Asia/Kolkata)';
List<String> lststrsplit = TimeZones.substring(12,TimeZones.length()).split('\\(',2);
string strTimeZone = lststrsplit[1].substring(0,lststrsplit[1].length()-1);
system.debug(CurentTime+'abc#');
string strCurrentTime = CurentTime.format('YYYY-MM-dd HH:mm:ss', strTimeZone);
Datetime dateCurrentTime = Datetime.valueof(strCurrentTime);