function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bhushan.AdhikariBhushan.Adhikari 

Reading a date of format 'EEE MMM dd HH:mm:ss Z yyyy' and converting to a proper dateTime format

Hello,

I am working with twitter integration and when i do a callout i get a response with a datestamp as 'Tue Jul 29 05:32:57 +0000 2014'. So I am looking for a way in salesforce to parse this date and convert it to a proper dateTime format. 
Any suggestions?

Regards,
Bhushan A
srlawr uksrlawr uk
Well, we do have very limited dateTime parsing tools within Apex in my experience, and in this case, my immediate twitch is probably just to say break the incoming stamp down as a string, and pass it into the DateTime method ValueOf()..

Find out a little about these methods here
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm#apex_System_Datetime_valueOf

This makes me feel a little gross, and I'm hoping another force.com expert with a little more spare brain space/experience can shed some light on this kind of parsing, but basically you may end up with something that looks like:

String year = twitterDate.substring(26, 30);

String month = twitterDate.substring(4, 8);
if(month.equals('Jan')) { month = '1'; }
if(month.equals('Feb')) { month = '2'; }
// etc.

String day = twitterDate.substring(8, 11);
String hour = twitterDate.substring(12, 14);
String minute = twitterDate.substring(15, 17);
String second = twitterDate.substring(18, 20);

String finalDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute +  ':' + second;

Datetime myDate = datetime.valueOf(finalDate);
(hand crafted code, may be missing a semi colon or I might have mis-counted an index for sure)

I hope twitter delivers dates in 01, 02, ... 16, 17 format, or you will get one character out, of course. If they do this, you might have to use string.split() and break your string up over whitespace, and then reference the relevant section instead for the array (you will still have to use substrings to break the time down around the colons (or another split()  !!!)

Either way, hacking a string up and passing it into valueOf() is my suggestion :)


Note: the finalDate string basically needs to match "“yyyy-MM-dd HH:mm:ss" and will be considered to be delivered/set in your local time zone (so you could always adjust that with the timezone in your twitter stamp?)