[SalesForce] How to convert date time to date

On a http call response i was getting date as 1970-01-08T23:45:27.461Z. Here i have to change it to date like 1970-01-08 … need some ideas…

when i tried to take Date type in wrapper classes directly it is throwing errors.. while displaying it to visual force pages….

    Date date="code to convert time stamp shown above to date "

Best Answer

First you need to normalise that string, then convert it to DateTime (there is already an apex function for that) of which you can easily obtain the date component. Here is a sample code:

String dateTimeString = '1970-01-08T23:45:27.461Z';
dateTimeString = dateTimeString.substring(0, dateTimeString.indexOf('.'));
dateTimeString = dateTimeString.replace('T', ' ');
Date myFinalDate = DateTime.valueOf(dateTimeString).date();