Hello friends, In this post, we will see an example to convert apex current time to epoch format also known as unix timestamp for saving date and time. So let's get started,

Convert datetime to epoch format in apex

To get the epoch from an apex datetime variable you can just call the getTime() instance method. The DateTime.getTime() method returns the number of milliseconds, so we’ll need to divide it by 1000 to get standard unix time.

Example:

 Datetime dt = Datetime.now();
 Long l = dt.getTime();
 System.debug(l);
 //divide by 1000 to get standard unix time
 Long epochformat = dt.getTime()/1000;
 System.debug('epochtime****'+epochformat);
Output:

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