All Things Techie With Huge, Unstructured, Intuitive Leaps
Showing posts with label get remaining minutes between two dates. Show all posts
Showing posts with label get remaining minutes between two dates. Show all posts

Java ~ How to get the remaining minutes between two dates


I had to look this up, so I am posting this snippet in the hope that it helps someone else.

I monitor events in real time. The program has to know when the event ends, and send SMS messages. The event, including the expiry date/time is a database entry. How do you determine the remaining time in minutes?

First I fetch the expiry date/time into a bean. It is stored as a GregorianCalendar entity in the database, because I have to extensively manipulate it before the event starts. Then I get a time for NOW. Then I have to subtract the two. This is how I do it:

Date now = new Date();
GregorianCalendar expiryDate = myevent.getExpiryDate();
Date end = expiryDate.getTime();
long difference = Math.abs(end.getTime() - now.getTime());
long minutesLeft = difference / (1000 * 60);

Hope this helps.