0

I am making rest api, In rest method I recieve Datetime parameter. In javascript datetime string looks this: "2015-12-01T12:44:00.000+0000" This is in the same format as I recieved from apex rest. My user's timezone is GMT+2. In apex rest method parameter value is this: "2015-12-01T14:44:00.000+0000". So it automatically parses it and adds 2 hours because of user locale. How can I avoid adding timezone when salesforce parces it without changing string pattern?

2
  • I have figured that in next rest call apex returns this new date in "2015-12-01T12:44:00.000+0000". So from javascript comes 12h, in salesforce it saves as 14, but again rest returns 12h Dec 2, 2015 at 10:09
  • If I change my timezone to GMT, it returns 12h in rest method and shows 12h in standard layout. So Salesforce does not convert datetime to user's time zone when returning via REST. Dec 2, 2015 at 10:26

1 Answer 1

1

If you look at your string, it says "2015-12-01T12:44:00.000+0000" Last 4 digit represent the timezone.

Try below code snippet to convert the string to DateTime.

String val = '2015-12-01T12:44:00.000+0000'; 

DateTime date1 = (DateTime)Json.deserialize('"'+val+'"', DateTime.class);

System.debug(date1)  ---> 2015-12-01 12:44:00

String dateformat = 'yyyy-MM-dd HH:mm:ss:sssZ';
String abc = date1.format(dateformat,''+userinfo.getTimeZone());
system.debug(abc);

Details about all parameters in date format string : https://paulforce.wordpress.com/2009/08/27/formatting-time-in-apex/

2
  • Thanks, but is there a way to deliver a date with timezone for user via apex rest? Dec 2, 2015 at 11:27
  • Check the updated code snippet. Z returns the local users timezone. Dec 2, 2015 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .