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 "
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();
  • 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 Apr 16 '15 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 – Boris Bachovski Apr 16 '15 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.