Reputation: 2233
Ok, I'm officially stumped on this one. I have a GregorianCalendar object that I would like to determine if it is in the past, present, or future. So far, the Calendar#compareTo docs are confusing to me, in that I am getting erratic results. A simple test class will illustrate my problem:
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MyCal
{
public static void main( String[] args )
{
GregorianCalendar datePast = new GregorianCalendar();
datePast.add(Calendar.MONTH, -6); // subtract 6 months
GregorianCalendar datePresent = new GregorianCalendar();
GregorianCalendar dateFuture = new GregorianCalendar();
datePast.add(Calendar.MONTH, 6); // add 6 months
System.out.println("compare datePresent to datePast: "+datePresent.compareTo(datePast));
System.out.println("compare datePresent to datePresent: "+datePresent.compareTo(datePresent));
System.out.println("compare datePresent to dateFuture: "+datePresent.compareTo(dateFuture));
}
}
And the output:
compare datePresent to datePast: 1
compare datePresent to datePresent: 0
compare datePresent to dateFuture: 0
My understanding of compareTo is that the last line should be a -1. Can anyone tell me what I'm doing wrong?
Upvotes: 1
Views: 2772
Reputation: 1500495
You've added 6 months to datePast
, not dateFuture
. Here's the working code:
import java.util.Calendar;
import java.util.GregorianCalendar;
public class MyCal
{
public static void main( String[] args )
{
GregorianCalendar datePast = new GregorianCalendar();
datePast.add(Calendar.MONTH, -6); // subtract 6 months
GregorianCalendar datePresent = new GregorianCalendar();
GregorianCalendar dateFuture = new GregorianCalendar();
dateFuture.add(Calendar.MONTH, 6); // add 6 months
System.out.println("compare datePresent to datePast: "+datePresent.compareTo(datePast));
System.out.println("compare datePresent to datePresent: "+datePresent.compareTo(datePresent));
System.out.println("compare datePresent to dateFuture: "+datePresent.compareTo(dateFuture));
}
}
Your results would have been inconsistent because sometimes dateFuture
would be later than datePresent
, depending on when the internal clock "ticked".
In addition to all of this, I can't help but give my standard Java date and time advice: use Joda Time instead. It's not responsible for this particular problem, although the fact that many of the types are immutable would have helped. But in general it's a much better library.
Upvotes: 2