4

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 "

1 Answer 1

4

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();
2
  • thank you... but myFinalDate is coming as 1970-01-08 00:00:00 i need something like 1970-01-08,i have achieved this with your idea substring dateTimeString = dateTimeString.substring(0, dateTimeString.indexOf('T')); this results only date without any time stamp... but return type is still String ...
    – Rajasekhar
    Commented Apr 16, 2015 at 6:08
  • 1
    In the last line of my code sample we're converting the datetime to date and storing it in a date type variable. If you need just the date as a string then you can either substring the first 10 characters of the original string, or you can use the date method .format() which will return a string of the date variable in the current user's locale Commented Apr 16, 2015 at 6:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .