Sunday, April 5, 2015

Convert String into Acceptable DateTime in Apex

// API Response DateTime = 2015-03-16T16:04:56.0000000+00:00
// Acceptable Format = 2015-03-16 16:04:56

// DateTime in String format
String dateTimeInString = '2015-03-16T16:04:56.0000000+00:00';

// Convert String into DateTime using "replace" method of String and "Valueof" method of DateTime
DateTime acceptableDateTime = DateTime.Valueof(dateTimeInString.replace('T', ' ')); 

System.Debug('>>Acceptable DateTime :<<'+acceptableDateTime);

Result

>>Acceptable DateTime :<< 2015-03-16 16:04:56