United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4655637 Calendar.set() for DAY_OF_WEEK does not return the right value
JDK-4655637 : Calendar.set() for DAY_OF_WEEK does not return the right value

Details
Type:
Bug
Submit Date:
2002-03-20
Status:
Resolved
Updated Date:
2003-04-12
Project Name:
JDK
Resolved Date:
2002-09-09
Component:
core-libs
OS:
windows_2000
Sub-Component:
java.util:i18n
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.4.1
Fixed Versions:
1.4.2 (mantis)

Related Reports

Sub Tasks

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
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
                                     
2002-03-22
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
                                     
2002-03-22
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
mantis
mantis-b02

FIXED IN:
mantis
mantis-b02

INTEGRATED IN:
mantis
mantis-b02


                                     
2004-06-14



Hardware and Software, Engineered to Work Together