+1 vote
30 views
by (163k points)

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 vote
by (163k points)
 
Best answer

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

Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...