+ Start a Discussion
Adam HowarthAdam Howarth 

String date (yyyy-MM-ddTHH:mm:ss.SSSZ) into date object

Hey guys,


I have got this working but it looks horrible!


Is there anything more efficient or out of the box to convert a string in the format yyyy-MM-ddTHH:mm:ss.SSSZ to a date object.


Tried parsing after removing the time, valueOf etc.


Heres my code:


// Parses the returned datetime (yyyy-MM-ddTHH:mm:ss.SSSZ)
private date ParseDateTime(string value)
String[] dateAndTime = value.split('T');
String[] dateParts = dateAndTime[0].split('-');

return date.newinstance(Integer.valueOf(dateParts[0]), Integer.valueOf(dateParts[1]),Integer.valueOf(dateParts[2]));








Try below code :-


DateTime Dt = System.today();
Date D = Date.newInstance(Dt.year(),Dt.Month(),Dt.day());
Adam HowarthAdam Howarth

I dont think you read the question properly.


I am well aware of converting a date time to a date.


The problem is it is a DateTime string in the format described in the title.


So I am calling my method like this:



Naidu PothiniNaidu Pothini
String str1 = '2012-03-08T10:51:12.421Z';

String str = str1.replace('T',' ').replace('Z', '');

Date dt = date.valueOf(str);

 Try this.