EN
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 answer
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);
}
}
0 comments
Add comment