[SalesForce] Datetime from string with user locale

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?

Best Answer

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/

Related Topic