Date/Time Conversion from YYYY-MM-DDTHH:MM:SS

389    Asked by DavidPiper in Data Science , Asked on Jul 14, 2021

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. How to convert  Date/Time  in YYYY-MM-DDTHH:MM:SS format?

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



Your Answer

Interviews

Parent Categories