Checking a Date is within a month of the current Date

Hi there,As the subject state. I have a problem which is to show all consultation bean objects whose deadline date (java.util.Date) is within a month of the current date.Any ideas much appreciated,
[218 byte] By [jonesy21a] at [2007-10-3 4:31:01]
# 1
Use the GregorianCalendar class.
pkwoostera at 2007-7-14 22:34:22 > top of Java-index,Java Essentials,Java Programming...
# 2

> As the subject state. I have a problem which is to

> show all objects whose date (java.util.Date) is within a month of the

> current date.

You haven't really defined your problem enough. Is that all dates within 30 days, 31 days, 28 days? Or are you saying that if it is the 5th of Feb, then I'm interested in all before 5th of March?

You can use the java.util.Calendar class to do date "arithmetic" and then getDate objects which you can compare... Post some code and we'll help you

oxbow_lakesa at 2007-7-14 22:34:22 > top of Java-index,Java Essentials,Java Programming...
# 3

I agree in the brief reply above.

Past or future? The following method tests if a date is between today (say, Sept 8) and a month later (say, October 8, inclusive). Go ahead and modify it to your needs.

public static boolean withinAMonth(Date d)

{ Calendar thisDayOfMonth = new GregorianCalendar(); // today

Calendar test = new GregorianCalendar();

test.setTime(d);

if (thisDayOfMonth.get(Calendar.YEAR) == test.get(Calendar.YEAR)

&& thisDayOfMonth.get(Calendar.MONTH) == test.get(Calendar.MONTH)

&& thisDayOfMonth.get(Calendar.DAY_OF_MONTH) <= test.get(Calendar.DAY_OF_MONTH))

// later this month

{ return true;

}

thisDayOfMonth.add(Calendar.MONTH, 1); // go to same day next month

return thisDayOfMonth.get(Calendar.YEAR) == test.get(Calendar.YEAR)

&& thisDayOfMonth.get(Calendar.MONTH) == test.get(Calendar.MONTH)

&& thisDayOfMonth.get(Calendar.DAY_OF_MONTH) >= test.get(Calendar.DAY_OF_MONTH);

}

OleVVa at 2007-7-14 22:34:22 > top of Java-index,Java Essentials,Java Programming...
# 4
oxbow_lakes is right, my code may display slightly unexpected behaviour if you run it on, say, January 30, since there is no February 30. It may be fine for some purposes, certainly will be unacceptable for others. As I said, modify it to your own needs.Thanks,Ole
OleVVa at 2007-7-14 22:34:22 > top of Java-index,Java Essentials,Java Programming...
# 5
One more thing, to work correctly in all cases, my code should also check if the ERA of the two Calendar objects is the same.Ole
OleVVa at 2007-7-14 22:34:22 > top of Java-index,Java Essentials,Java Programming...
# 6

I know I'm reviving an old thread now. I just happened to look at my code again and think it was unnecessarily complicated. So here's a new attempt (not thoroughly tested):

public static boolean withinAMonth(Date d)

{

// System.out.println(DateFormat.getDateInstance().format(d));

Calendar thisDayOfMonth = new GregorianCalendar(); // today

Calendar test = new GregorianCalendar();

test.setTime(d);

if (test.before(thisDayOfMonth))

{ return false;

}

thisDayOfMonth.add(Calendar.MONTH, 1); // go to same day next month

return ! test.after(thisDayOfMonth);

}

OleVVa at 2007-7-14 22:34:22 > top of Java-index,Java Essentials,Java Programming...