I have written an apex class which calls an API and it returns a response. One of the values in the response is a string value representing date and time. The date time is in the format 2022-08-28T12:35:30 format for example. I just want the 2022-08-28 part and display it in the format 08-28-2022. How do I do that? If I store the response in the variable "Update time" then would it be

Date Update Time = response.format()? Can someone please help me convert 2022-08-28T12:35:30 to 08-28-2022 ? Thank you

  • Does this answer your question? Convert DateTime to Date Commented Aug 30, 2022 at 15:10
  • What have you tried? Where are you stuck? Is there a particular reason why you don't want to use the standardized ISO 8601 format (YYYY-MM-dd)? Also, where are you trying to display this date (is stored in an SObject field and displayed on the page layout? is it on a visualforce page/LWC?)
    – Derek F
    Commented Aug 30, 2022 at 15:16

2 Answers 2


We can't change the way the date is represented in the Date class, but we can format it a certain way when we convert to a string. This can be accomplished with the Datetime class format() method':

String formatted = response.format('MM-dd-yyyy');

here is the updated code:

String response = '2022-08-28T12:35:30';
response = response.replace('T' , ' ' );
DateTime myDate= Date.valueof(response);
String dateStr = myDate.format('MM-dd-yyyy') ;
  • One problem i just noticed that the response is a string text which is in a date format. so i need to take the string 2022-08-29T12:00:07 and remove the T Commented Aug 30, 2022 at 19:23
  • I have updated my answer. Commented Aug 31, 2022 at 19:01
  • @user3688231 can you provide the response you are getting in String? Commented Sep 1, 2022 at 6:57

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