Top community members
All Questions Ask Question

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

Calculate link confirmation timeout 24h in java

1 answers
3 points

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 answers
1 points

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);
        System.out.println(differenceInMinutes);

        // 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

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more