Solved

How to calculate the number of days between two Dates?

Posted on 2009-05-07
8
1,855 Views
Last Modified: 2012-05-06
I'm trying to write a function that will calculate the number of days that exist between two dates,  but have been unable to come up with an adequete solution thus far. In the reasearch that I've done, I've seen some methods convert the dates to milliseconds, subtract, then convert back. In others, they've incrimented a count variable, and simple added days to the earlier date until the two dates matched. Unfortunately, neither of these methods seems completely accurate under all conditions, nor do they seem as elegant as you'd hope them to be.

How have you done this calculation before? Is there a common library or standard function that's commonly used for this calculation?

Thanks in advance.
0
Comment
Question by:MehtaJasmin
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24324928
0
 
LVL 16

Expert Comment

by:CuteBug
ID: 24324931
0
 
LVL 92

Expert Comment

by:objects
ID: 24332244
the problem is more in the definition of what you mean by the number of days between two dates. Once that is defined then implementation is generally straight forward. Using a Calendar for the arithmetic is the standard way, and there are also libraries around.

So what is your definition of the number of days between 2 dates?

0
 

Author Comment

by:MehtaJasmin
ID: 24335002
My requirement is, if I pass dateA and dateB to the some java method /  API, it should return me how many days they are apart. It should consider if they in the same year/leap year etc. Basically I am coding for password expiry logic where I need to check that last time user has updated password, has it been 60 days yet. If current date is exceeding the 60 day period of validity then I need to prompt user to change the password.

I have dig in to java.util.Calendar API but could not find appropriate method. Also tried writing test program using DateUtils of commons package with getFragmentInDays() and iterator(java.util.Calendar focus,  int rangeStyle). But looks like I need to write quite a bit of math around iterator() to get the result out. So I was looking for any straight api / jar available for my need.

Hope this defines the question better.

Hope this clears the question.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 24335051
Easiest way to do that is as well (or instead of) storing the last updated date, you store the expiry date. Then you just have to check if the current date is after the expiry date.
And if you don't store it you can just use the Calendar class to add the 60 day period to the last updated date and then compare with current date. Much simpler that calculating differences which is messy.

http://helpdesk.objects.com.au/java/how-do-i-add-a-specified-number-of-hours-to-a-java-date

(Use Calendar.DATE instead of Calendar.HOUR  to add days)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24335080
For your two Date instances:
final int TIME_OUT_DURATION = 1000 * 60 * 60 * 24 * 60;

boolean needToChangePassword = (d1.getTime() - d2.getTime()) > TIME_OUT_DURATION;

Open in new window

0
 

Author Closing Comment

by:MehtaJasmin
ID: 31580092
Thanks! Actually had the same idea about the time you posted it. It's a much easier solution to our exact situation than attempting to calculate the time between two arbitrary dates. Thanks again!
0
 
LVL 92

Expert Comment

by:objects
ID: 24353273
thanks mate, let me know if you need any other help.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Securing Jmx Console and web console 2 64
split string containing \r\n in Java 46 38
more than one jdk and one jre 1 32
java stored proc example 9 21
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now