views:

155

answers:

4

What is the best way to count the amount of time between two Calendar dates in java. I am writing a method that determines the number of months that pass between two dates and returns a boolean based on a predefined term of months. This is my code(does not work correctly).

This code always returns false. Also this code does not take into account the number of days passed. This could be a problem if the start date is at the end of a month. Is there not a simple compareTo method?

private boolean hasMatured()
{
    Calendar now = Calendar.getInstance();

    Calendar start = (Calendar) super.dateOpened.clone();

    int nowYear = now.get(Calendar.YEAR);
    int nowMonth = now.get(Calendar.MONTH);

    int startYear = start.get(Calendar.YEAR);
    int startMonth = start.get(Calendar.MONTH);

    int monthsElapsed = (nowYear - startYear) * 12 + (nowMonth - startMonth);

    return monthsElapsed>PERIOD_IN_MONTHS;
}
+2  A: 
int nowYear = now.get(Calendar.YEAR);
int nowMonth = now.get(Calendar.MONTH);

int startYear = now.get(Calendar.YEAR);
int startMonth = now.get(Calendar.MONTH);

int monthsElapsed = (nowYear - startYear) * 12 + (nowMonth - startMonth);
danben
A: 

The code looks fine expect from one major caveat: Calendar is mutable.

So, instead of

Calendar start = super.dateOpened;

you should have done

Calendar start = (Calendar) super.dateOpened.clone();

otherwise the changes get reflected in dataOpened which may cause unexpected side-effects.

BalusC
I'm not familiar with the term mutable.However, I understand why it is important to clone the object. I was only passing the reference to the start object, and that is why the changes would be reflected. Thank you for the help.
ZeroDivide
+1  A: 

I would strongly recommend Joda Time for all date-related stuff in Java. It has a much cleaner and more intuitive API, togather with the concepts of intervals between dates etc.

Brian Agnew
Thank you! I will check this out. However, this code is for a CS assignment and I believe that we are supposed to use the java API.
ZeroDivide
Fair enough. Perhaps you'll get extra marks by mentioning that java.util.Calendar isn't really ideal :-)
Brian Agnew
Funny how the "correct" CS answer is to ignore the well built and tested free library and to instead become an overnight expert at date math. Oh, the irony.
Chris Nava
@Brian Agnew - If I use the Joda Time API, would I have to package it with the final build somehow?
ZeroDivide
Yes. You'll need to provide the .jar alongside your solution
Brian Agnew
A: 

Am I missing something? There is a compareTo() in Calendar, as well as other useful stuff...

How about:

Calendar now = Calendar.getInstance();

now.add(Calendar.MONTH, -PERIOD_IN_MONTHS);

return super.dateOpened.before(now);

Subtract X months from today, and see if the start date is still before that date. If it is, then X months must have passed.

Erik
The OP did that before, but changed it afterwards to reflect the given answers. Check the edit history (click at link after `edited` in the question).
BalusC