Calculate link confirmation timeout 24h in java

3 points
Asked by:

How can I calculate the timeout logic between database entity and current time.
The expiration should be 24h.
I need to return information to the user that the link expired after time elapsed.

I have link confirmation creation time like this: 

Date creationTime;

Result message will be: Confirmation link was active 24h.

How to calculate the difference?

1 answer
1 points
Answered by:

We need to calculate the difference in minute between the Date from database and current Date. If the difference in minutes is greater then 1440 minutes (1 day) then we return the expiration message, if not then link is still active.

Code which solves the problem in question:

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;

public class Solution {

    public static void main(String[] args) {

        // your time from database
        LocalDateTime of1 = LocalDateTime.of(2020, 1, 20,  16,   20,  0,  0);
        Date from = Date.from(of1.toInstant(ZoneOffset.UTC));
        Date now  = new Date(); // current time

        long differenceInMinutes = getDifferenceInMinutes(from, now);

        // 24 h * 60 min = 1440 min
        if (differenceInMinutes > 1440) {
            System.out.println("Confirmation link was active 24h.");
        } else {
            System.out.println("Link is still valid.");

    public static long getDifferenceInMinutes(Date from, Date now) {
        long diff = now.getTime() - from.getTime();
        return diff / (60 * 1000);

Link to similar problem

0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂