Hello friends, In this post you will learn, how to convert a datetime to GMT format string as well as you can also find an example to convert datetime to local time zone. So let's get started,

Convert datetime to GMT format in apex

Here I am going to provide two examples of datetime conversion that use current datetime.

1. Conversion in local time zone
 String formatedInLocalTimeZone = Datetime.now().format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
 System.debug('In Local TimeZone^^^'+formatedInLocalTimeZone);
Output:



2. Conversion in GMT 
 String formatedInGMT = Datetime.now().formatGMT('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
 System.debug('In GMT^^^'+formatedInGMT);
Output:

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