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
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]));
}

 

 

Cheers,

Adam

Vinit_KumarVinit_Kumar

Hi,

 

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:

 

 ParseDateTime('2012-03-08T10:51:12.421Z');

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.