Convert an offset in seconds to Datetime in Apex
We often come across a challenge where we want to calculate a future (or a past) Date from the current Date based on an offset that are in seconds.
For e.g., in an OAuth flow once we receive an
access_token from an
authorization_code grant type, we also get an expiry time in seconds - this is basically the offset/duration after which the
access_token will expire. When the
access_token expires, the OAuth flow gives an option to refresh the token using
Therefore we need to know when the
access_token will expire - not in seconds but the Datetime relative to the Datetime when the
access_token was generated.
It isn't a very complex calculation but a bit complex for formula field. Here is a sample apex code to calculate the future Datetime offset from current Datetime:
Apex code to convert seconds to Datetime
This post is for paying subscribers only
Sign up now and upgrade your account to read the post and get access to the full library of posts for paying subscribers only.
Sign up now
Already have an account? Sign in
You’ve successfully subscribed to Inteygrate
Welcome back! You’ve successfully signed in.
Great! You’ve successfully signed up.
Your link has expired
Success! Check your email for magic link to sign-in.