I am getting the datetime value from the 3rd party. But when i try to store in the salesforce datetime field it gets stored in different TimeStamp.

For example :- 2022-08-09 14:40:00 -0400

I am getting the above value in the string format. But the correct time is 2022-08-09 10:40:00. How should i store in the salesforce datetime field with the correct timeStamp.

String billingDate = '2022-08-09 14:40:00 -0400';

DateTime dateTimeConvert = DateTime.valueOf(billingDate);

Integer valueOfHour = dateTimeConvert.hour();

Integer hourChange = Integer.valueOf(billingDate.substring(20, 23));

Integer changeInHour = valueOfHour + hourChange;

Opp.customDateTimeField__c = DateTime.newInstance(dateTimeConvert.year(),dateTimeConvert.month(),dateTimeConvert.day(),changeInHour,dateTimeConvert.minute(),dateTimeConvert.second());

Any help would be appreciated.

  • What does -0400 mean? – Avinash Sep 9 '16 at 15:08
  • This is coming from the 3rd party. I guess this is the TimeZone i am not sure. – Abhinav Bhatia Sep 9 '16 at 15:09
  • Without understanding input pattern, it is not possible to format it. – Avinash Sep 9 '16 at 15:11
  • I guess this the timeZone difference they are sending. If we will subtract the same number of hour we will get the same time. – Abhinav Bhatia Sep 9 '16 at 15:13
  • But even after i subtract and try to save in the datetime field the time gets changed. – Abhinav Bhatia Sep 9 '16 at 15:13

Try this:

String billingDate = '2022-08-09 14:40:00 -0400';
DateTime dateTimeConvert = DateTime.valueOfGMT(billingDate);
String testDate = dateTimeConvert.format(''yyyy-MM-dd HH:mm:ss','America/New_York');

Debug value : 08:30:36:003 USER_DEBUG [4]|ERROR|**testDate***2022-08-09 10:40:00

  • But when i store the above value after converting into date/time in date/time field of the opportunity it show different time. It get changed again. – Abhinav Bhatia Sep 14 '16 at 13:52

I think this might help: Datetime won't save using correct time zone

You will need to format your input string to add a T between the date and time parts, and to remove the space before the timezone offset.

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.