Hello friends, In this post you see a code example that helps you to convert a datetime string in valid datetime value in apex. I thought most of our friends are facing datetime related issues. so I decided to do a little help using this post.

Datetime string to valid datetime value

In many scenario, We need to convert a string of datetime to acceptable datetime format to save data in salesforce object. You can use below example to convert your string datetime to valid datetime in apex.

Example:

 String sDatetime='2021-10-16T15:20:41.990679+00:00';
Datetime validDatetime=DateTime.Valueof(sDatetime.replace('T', ' '));
System.debug('validDatetime^^ '+validDatetime);

Output:


Note:
Don't confuse with output. It's show as per my org timezone setting.

Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.