Re: Date comparisons



On 12-03-2010 11:12, laredotornado wrote:
I'm using Java 1.5. I have a java.util.Date object and I would like
to determine if it's date (i.e. year, month, and day) are greater than
(in the future) or equal to today's date (year, month, and day).
However, I don't care about any time component (hour, minute,
second ...) when the comparison is taking place. What is the easiest
way I can determine this?

There must be several ways.

My suggestion:

public static boolean isFutureDay(Date d) {
Calendar cal = Calendar.getInstance();
cal.setTime(d);
Calendar fut = Calendar.getInstance();
fut.add(Calendar.DATE, 1);
fut.set(Calendar.HOUR_OF_DAY, 0);
fut.set(Calendar.MINUTE, 0);
fut.set(Calendar.SECOND, 0);
fut.set(Calendar.MILLISECOND, 0);
return cal.compareTo(fut) >= 0;
}

Arne
.