###@###.### 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();
}
}