1

I have a Date/Time value from a JSON response in the form of 2020-10-01T15:22:20 that I am trying to convert to something that looks like 10-01-20 15:22 in Apex. Any help is greatly appreciated.

1 Answer 1

3

To do this, you can use the apex DateTime class:

// replace the 'T' with blank space to make date time string compatible with format method
String dtString = '2020-10-01T15:22:20'.replace('T', ' ');

// Convert the string to DateTime
DateTime dt = DateTime.valueOfGMT(dtString);

// Now use formatGMT method with appropriate dateFormatString to convert DateTime to string 
String dtNewString = dt.formatGMT('MM-dd-yy HH:mm');

System.debug(dtNewString);

In the console this outputs as:

DEBUG|10-01-20 15:22

1
  • That worked, thank you Raul!
    – Al K
    Commented Oct 8, 2020 at 1:11

You must log in to answer this question.

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