• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Help to understand Calendar.roll() behavour

 
Md. Minhajur Rahman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, I have been studying Calendar Class, couldn't understand the roll function behavour regarding the output of the following code snippet. I have figured out that calendar.roll(Calendar.WEEK_OF_MONTH,-1) decrease days by 7. what is happening after line 20. Please help..



Output:

Tue Jun 15 07:45:24 ACT 1999
Tue Jun 08 07:45:24 ACT 1999
Thu Jun 10 07:45:24 ACT 1999
Thu Jun 03 07:45:24 ACT 1999
Tue Jun 08 07:45:24 ACT 1999
Tue Jun 01 07:45:24 ACT 1999
Sun Jun 06 07:45:24 ACT 1999
Tue Jun 01 07:45:24 ACT 1999
Sat Jun 05 07:45:24 ACT 1999
Wed Jun 30 07:45:24 ACT 1999
Fri Jun 04 07:45:24 ACT 1999
Wed Jun 30 07:45:24 ACT 1999
Wed Jun 02 07:45:24 ACT 1999
Wed Jun 30 07:45:24 ACT 1999
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Md. Minhajur Rahman wrote:I have figured out that calendar.roll(Calendar.WEEK_OF_MONTH,-1) decrease days by 7.


That assumption isn't actually correct
If you look at the API documentation of the Calendar class, specifically the documentation of the WEEK_OF_MONTH field, it should be clear what that time field actually represents.
Also, the first line of the documentation of the roll(int,int) method is rather significant.
 
Md. Minhajur Rahman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot @Jelle Klap. I have been looking around your reference API documentation of the Calendar class the documentation of the WEEK_OF_MONTH field, but i couldn't understand clearly there.But rather i have found some idea here at GregorianCalendar. There is told that
Values calculated for the WEEK_OF_MONTH field range from 0 to 6. Week 1 of a month (the days with WEEK_OF_MONTH = 1) is the earliest set of at least getMinimalDaysInFirstWeek() contiguous days in that month, ending on the day before getFirstDayOfWeek(). Unlike week 1 of a year, week 1 of a month may be shorter than 7 days, need not start on getFirstDayOfWeek(), and will not include days of the previous month. Days of a month before week 1 have a WEEK_OF_MONTH of 0.

For example, if getFirstDayOfWeek() is SUNDAY and getMinimalDaysInFirstWeek() is 4, then the first week of January 1998 is Sunday, January 4 through Saturday, January 10. These days have a WEEK_OF_MONTH of 1. Thursday, January 1 through Saturday, January 3 have a WEEK_OF_MONTH of 0. If getMinimalDaysInFirstWeek() is changed to 3, then January 1 through January 3 have a WEEK_OF_MONTH of 1.


Yeah, I've got the idea of WEEK_OF_MONTH. and roll function does not affect larger part (Month,Year), for rolling WEEK_OF_MONTH , but may affect smaller part DAY_OF_WEEK.
So accordingly my program, getMinimalDaysInFirstWeek() is 1. If getFirstDayOfWeek() is SUNDAY. For the case

At line 5, calendar.set(1999,Calendar.JUNE,15);
WEEK_OF_MONTH is 3 and System.out.println(calendar.getTime()) is Tue Jun 15 07:45:24 ACT 1999.
After calling calendar.roll(Calendar.WEEK_OF_MONTH,-1) ,
WEEK_OF_MONTH is 2 and System.out.println(calendar.getTime()) is Tue Jun 08 07:45:24 ACT 1999.
// DAY_OF_WEEK is not affected and DAY_OF_MONTH is 08 i.e is decreased by 7.

At line 10, calendar.set(1999,Calendar.JUNE,10);
WEEK_OF_MONTH is 2 and System.out.println(calendar.getTime()) is Thu Jun 10 07:45:24 ACT 1999
After calling calendar.roll(Calendar.WEEK_OF_MONTH,-1) ,
WEEK_OF_MONTH is 1 and System.out.println(calendar.getTime()) is Thu Jun 03 07:45:24 ACT 1999
// DAY_OF_WEEK is not affected and DAY_OF_MONTH is 03 i.e is decreased by 7.

At line 15, calendar.set(1999,Calendar.JUNE,8);
WEEK_OF_MONTH is 2 and System.out.println(calendar.getTime()) is Tue Jun 08 07:45:24 ACT 1999
After calling calendar.roll(Calendar.WEEK_OF_MONTH,-1) ,
WEEK_OF_MONTH is 1 and System.out.println(calendar.getTime()) is Tue Jun 01 07:45:24 ACT 1999
// DAY_OF_WEEK is not affected and DAY_OF_MONTH is 01 i.e is decreased by 7.

At line 20, calendar.set(1999,Calendar.JUNE,6);
WEEK_OF_MONTH is 2 and System.out.println(calendar.getTime()) is Sun Jun 06 07:45:24 ACT 1999
After calling calendar.roll(Calendar.WEEK_OF_MONTH,-1) ,
WEEK_OF_MONTH is 1 and System.out.println(calendar.getTime()) is Tue Jun 01 07:45:24 ACT 1999
// DAY_OF_WEEK is affected and DAY_OF_MONTH is 01 i.e is decreased by 5.

At line 25, calendar.set(1999,Calendar.JUNE,5);
WEEK_OF_MONTH is 1 and System.out.println(calendar.getTime()) is Sat Jun 05 07:45:24 ACT 1999
After calling calendar.roll(Calendar.WEEK_OF_MONTH,-1) ,
WEEK_OF_MONTH is 5 and System.out.println(calendar.getTime()) is Wed Jun 30 07:45:24 ACT 1999
// DAY_OF_WEEK is affected and DAY_OF_MONTH is 30 i.e is decreased by 5.

At line 30, calendar.set(1999,Calendar.JUNE,4);
WEEK_OF_MONTH is 1 and System.out.println(calendar.getTime()) is Fri Jun 04 07:45:24 ACT 1999
After calling calendar.roll(Calendar.WEEK_OF_MONTH,-1) ,
WEEK_OF_MONTH is 5 and System.out.println(calendar.getTime()) is Wed Jun 30 07:45:24 ACT 1999
// DAY_OF_WEEK is affected and DAY_OF_MONTH is 30 i.e is decreased by 4.

At line 35, calendar.set(1999,Calendar.JUNE,2);
WEEK_OF_MONTH is 1 and System.out.println(calendar.getTime()) is Wed Jun 02 07:45:24 ACT 1999
After calling calendar.roll(Calendar.WEEK_OF_MONTH,-1) ,
WEEK_OF_MONTH is 5 and System.out.println(calendar.getTime()) is Wed Jun 30 07:45:24 ACT 1999
// DAY_OF_WEEK is not affected and DAY_OF_MONTH is 30 i.e is decreased by 2.

And Again if calendar.set(1999,Calendar.JUNE,1);
WEEK_OF_MONTH is 1 and System.out.println(calendar.getTime()) is Tue Jun 01 01:04:42 ACT 1999
After calling calendar.roll(Calendar.WEEK_OF_MONTH,-1) ,
WEEK_OF_MONTH is 5 and System.out.println(calendar.getTime()) is Tue Jun 29 01:04:42 ACT 1999
// DAY_OF_WEEK is not affected and DAY_OF_MONTH is 29 i.e is decreased by 2.

Sometimes DAY_OF_WEEK is affected. but DAY_OF_MONTH is always affected.
My questions is now, Is DAY_OF_MONTH affected because of resolving DAY_OF_WEEK or vise versa? When, Why and How Resolving of the DAY_OF_WEEK/DAY_OF_MONTH is done?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic