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
Edward Abraham NalakurthiEdward Abraham Nalakurthi 

Convert String(2016-02-04T22:21:32.169Z) to DateTime

HI 

I have date in 2016-02-04T22:21:32.169Z format.How to convert it to Apex DateTime?

Ed
Best Answer chosen by Edward Abraham Nalakurthi
Edward Abraham NalakurthiEdward Abraham Nalakurthi
Thank you Simit for your response . Below link has simple solution 

https://developer.salesforce.com/forums/?id=906F000000090P7IAI

 

All Answers

RitikRitik
Try Date myDate = date.valueOf(stringDate);

refer valueOf in this link https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm

Best,
Ritk
Edward Abraham NalakurthiEdward Abraham Nalakurthi
Thank you Ritk.I tried this , time is resetting to  00:00:00.I need it in Apex DateTime 

Ed
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Edward Abraham Nalakurthi, 
You can try this -
String StringTime = '2016-02-04T22:21:32.169Z';
List<String> DateAndTimeList = StringTime.split('T');
List<String> dayList = DateAndTimeList[0].split('-'); 
List<string> timeList = DateAndTimeList[1].split(':');
DateTime dt = DateTime.newInstance(Integer.valueOf(dayList[0]), Integer.valueOf(dayList[1]), Integer.valueOf(dayList[2]), Integer.valueOf(timeList[0]), Integer.valueOf(timeList[1]), Integer.valueOf(timeList[2].split('\\.')[0]));
System.debug('DateTime is : '+dt);
Pls, Let me know If it helps you. 

Thnaks,
Sumit Kumar Singh
 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Alternatively,
You can write a method to convert Sting into DateTime, and use that method where ever you want. 
public DateTime toDateTime(String DateTimevalue) {
    List<String> DateAndTimeList = DateTimevalue.split('T');
    List<String> dayList = DateAndTimeList[0].split('-');
    List<string> timeList = DateAndTimeList[1].split(':');
    DateTime dt = DateTime.newInstance(Integer.valueOf(dayList[0]), Integer.valueOf(dayList[1]),         Integer.valueOf(dayList[2]), Integer.valueOf(timeList[0]), Integer.valueOf(timeList[1]),  Integer.valueOf(timeList[2].split('\\.')[0]));
    return dt;
}

Thanks, 
SumitKumar Singh
Edward Abraham NalakurthiEdward Abraham Nalakurthi
Thank you Simit for your response . Below link has simple solution 

https://developer.salesforce.com/forums/?id=906F000000090P7IAI

 
This was selected as the best answer
Gayatri Devarapalli 14Gayatri Devarapalli 14
to get the datatime;

(DateTime)json.deserialize('"YYYY-mm-ddThh:mm:ss.SSSZ"', datetime.class)
EX:(DateTime)json.deserialize('"2020-07-10T02:48:37.327Z"', datetime.class