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?