I'm new to Salesforce development and not sure why when I read a database column as string my result in debug log is:

01/06/2016 10:09 AM

while when i change it to DateTime it becomes:

2016-01-06 15:09:00

Why is there difference in the two? Any insights will be helpful. Here is my code:

String lastRunTime = myFeature.getAttributeValue('MyDate');
DateTime testDate = DateTime.parse(lastRunTime);
System.Debug('----------------------------------------------' + testDate);
System.Debug('----------------------------------------------' + lastRunTime);
  • What is myFeature? I'm guessing an Dom.XmlNode? – sfdcfox Jan 6 '16 at 20:20

You are having issues with time zone conversion. Try using Datetime.newInstanceGmt to get around them.

String lastRunTime = '01/06/2016';
Datetime lastRun = Datetime.parse(lastRunTime);
Datetime lastRunGmt = Datetime.newInstanceGmt(lastRun.date(), lastRun.time());

For me these result in:

lastRun: 2016-01-06 17:09:00
lastRunGmt: 2016-01-06 10:09:00
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.