I need to convert the string getting from JSON (For eg: 2014-08-26T17:50:20.000-0700) to this format '6/9/2014 11:57' to store in field which is of text format in salesforce.

Also per my requirement the field which i store date should not be in date/time format.


What about to use something like this that also handle the timezone (in case daylight saving changes too )

Datetime GMTDate = Datetime.newInstance(myDate.year(),myDate.year().month(), myDate.year().day(),0,0,0);
String strConvertedDate = GMTDate.format('MM/dd/yyyy HH:mm','America/Mexico_City');
  • Note you should use M/d instead of MM/dd because the OP requested 6/9, not 06/09.
    – Adrian Larson
    Jan 4 '17 at 14:10

You can use

Date dt = Date.valueOf('2014-08-26T17:50:20.000-0700');

String dts = DateTime.newInstance(dt.year(), dt.month(), dt.day()).format('d-MM-YYYY');

System.debug(logginglevel.error, dts);

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.