JDK-4682471 : API: Calendar.before() and after() should throw Exception
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2002-05-09
  • Updated: 2002-08-29
  • Resolved: 2002-08-29
Related Reports
Duplicate :  
Description

Name: nt126004			Date: 05/09/2002


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
The non-static methods Calendar.after(Object) and
Calendar.before(Object) behave inappropriately when passed
an object that is not an instance of Calendar; they both
return false in that case.

This is confusing if you are passing, for example, a Date
object, and the method just returns false, without an
exception.

I would suggest that either the signature of the before()
and after() methods be changed to receive a Calendar
object, or that they throw a ClassCastException when passed
an object which is not an instance of Calendar.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Build a Calendar object, set its YEAR field to 2002, its
MONTH field to Calendar.AUGUST, and its DAY_OF_MONTH field
to 5.  I'll call this object "cal".

2. Build a new Date() using the default constructor.  I'll
call this object "dat".

3. Now check the result of cal.after(dat).  It will return
false, even though August 5th is indeed after the current
date.

EXPECTED VERSUS ACTUAL BEHAVIOR :
The current result is for the methods to return false.

I would suggest either to:

-change the signature of the before() and after() methods
to receive a Calendar object instead of an Object object.

OR

-have the before() and after() methods throw a
ClassCastException when passed an object which is not an
instance of Calendar.

This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.*;

/**
 * @author Guillaume Guay
 */
public class CalendarTester {

	/**
	 * Default constructor.
	 */
	public CalendarTester() {
		super();
	}

	/**
	 * Starts the test.
	 * @param args command-line arguments, unused
	 */
	public static void main(String[] args) {
		Calendar cal = new GregorianCalendar();

		cal.set(Calendar.YEAR, 2002);
		cal.set(Calendar.MONTH, Calendar.AUGUST);
		cal.set(Calendar.DAY_OF_MONTH, 5);

		// this outputs false because we use a date, even though it is
before cal.
		System.out.println(cal.after(new Date()));
	}
}

---------- END SOURCE ----------

CUSTOMER WORKAROUND :
The obvious workaround is to pass the methods a Calendar
object...
(Review ID: 146326) 
======================================================================

Comments
EVALUATION The methods should accept Calendar and Date instances. Otherwise, throw an IllegalArgumentException. This fix requires approval as API changes. ###@###.### 2002-08-29 This problem should be fixed with other problems for Calendar comparison. Closing this as a dup of 4738710. ###@###.### 2002-08-29
29-08-2002