JDK-4655637 : Calendar.set() for DAY_OF_WEEK does not return the right value
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2002-03-20
  • Updated: 2003-04-12
  • Resolved: 2002-09-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.2 mantisFixed
Description
###@###.### 2002-03-20

J2SE Version (please include all output from java -version flag):
C:\j2sdk1.4.1\bin>java -version
java version "1.4.1-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b04)
Java HotSpot(TM) Client VM (build 1.4.1-beta-b04, mixed mode)


Does this problem occur on J2SE 1.3 or 1.4?  Yes / No (pick one)
Yes - tested with 1.3.1_02, problem was the same.


Operating System Configuration Information (be specific):
Windows 2000


Hardware Configuration Information (be specific):
Intel pentium III - 256 MB Ram (IBM 300 PL computer)


Bug Description:

Setting the Calendar.DAY_OF_WEEK can give problems in specific cases.



Steps to Reproduce (be specific):


Execute the test program and you will see the following output (will be a bit different if you execute it on a different day):

date: Tue Mar 19 13:24:18 CET 2002
year changed - date: Mon Mar 19 13:24:18 CET 2001
month changed - date: Fri Jan 19 13:24:18 CET 2001
day of month changed - date: Mon Jan 08 13:24:18 CET 2001
day of week changed: Mon Jan 22 13:24:18 CET 2001


The last line shows the bug. I set the day of the week to Monday 
(the Calendar is already on a Monday) and it changes the date to the 22nd, 
in stead of staying at January 8th.
But if you add the following line:
   System.out.println("day of week is " + cal.get(Calendar.DAY_OF_WEEK));
Before you set the day of the week, you will get the right result:

date: Wed Mar 20 13:31:38 PST 2002
year changed - date: Tue Mar 20 13:31:38 PST 2001
month changed - date: Sat Jan 20 13:31:38 PST 2001
day of month changed - date: Mon Jan 08 13:31:38 PST 2001
day of week is 2
day of week changed: Mon Jan 08 13:31:38 PST 2001
day of week is 2


----Start test program -------------------------------------------
import java.util.Calendar;

/**
 *
 * @author wimd
 */
public class TestCalendar
{
	public TestCalendar()
	{
		//Get day of today
		Calendar cal = Calendar.getInstance();
		System.out.println( "date: " + cal.getTime()); //prints the time this is executed

		//change the year to 2001
		cal.set(Calendar.YEAR, 2001);
		System.out.println( "year changed - date: " + cal.getTime());

		//change the month to January
		cal.set(Calendar.MONTH, Calendar.JANUARY);
		System.out.println( "month changed - date: " + cal.getTime());

		//change the date to 8
		cal.set(Calendar.DAY_OF_MONTH, 8);
		System.out.println( "day of month changed - date: " + cal.getTime());
		//The previous line should print: Mon Jan 08 13:19:44 CET 2001


		//We now set the day of the week to Monday, notice that it is already
		//a Monday so nothing should change I guess.
//* System.out.println("day of week is " + cal.get(Calendar.DAY_OF_WEEK)); */
//if you comment out this line, you will get the right result
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		System.out.println( "day of week changed: " + cal.getTime());
		//The previous line prints: Mon Jan 22 13:19:44 CET 2001
		//Why does the date change to January, 22th???
	}

	public static void main( String[] args )
	{
		new TestCalendar();
	}
}


Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mantis mantis-b02 FIXED IN: mantis mantis-b02 INTEGRATED IN: mantis mantis-b02
14-06-2004

WORK AROUND Calendar expects the following combinations of the fields to determine a date. MONTH + DAY_OF_MONTH MONTH + WEEK_OF_MONTH + DAY_OF_WEEK MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK DAY_OF_YEAR DAY_OF_WEEK + WEEK_OF_YEAR When you set DAY_OF_WEEK, the calendar expects a week field (WEEK_OF_MONTH, DAY_OF_WEEK_IN_MONTH or WEEK_OF_YEAR) has also been set. So, avoid setting DAY_OF_WEEK without setting one of the week fields. ###@###.### 2002-03-22
22-03-2002

EVALUATION When computing time (milliseconds), GregorianCalendar leaves some fields inconsistent. Then, after the last set(DAY_OF_WEEK), an invalid (older) WEEK_OF_MONTH value is used in the last getTime() call. ###@###.### 2002-03-22 The field values which are not to used to calculate time are invalidated in computeTime(). ###@###.### 2002-08-06
22-03-2002