Odd behaviour in Calendar object

Here i am trying to get the monday of week for a given day, for example

the day is 20th July 2007, so the result should be 16th July 2007.

see the following code

import java.util.*;

public class GetMonday {

public static void main(String[] args) {

int lStartDay = 20;

int lStartMonth = 6;

int lStartYear = 2007;

Calendar lCalendar = Calendar.getInstance();

lCalendar.set(Calendar.YEAR,lStartYear);

lCalendar.set(Calendar.MONTH, lStartMonth);

lCalendar.set(Calendar.DATE, lStartDay);

lCalendar.set(Calendar.HOUR_OF_DAY, 0);

lCalendar.set(Calendar.MINUTE, 0);

lCalendar.set(Calendar.SECOND,0);

lCalendar.getTime(); // Hack otherwise it will show the wrong date.

lCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

System.out.println(" lCalendar = "+lCalendar.getTime());

}

}

see the line number 17

lCalendar.getTime(); // Hack otherwise it will show the wrong date.

this line is apparently not required but if you comment out the line you will get

the output 30th July 2007 , very strange !!!

Any body has answer to this ?

[1183 byte] By [sukanta123a] at [2007-11-27 8:57:34]
# 1
In your code , there is the mistake on "lCalendar.set(Calendar.DATE, lStartDay);"huh,It would be "lCalendar.set(Calendar.DAY_OF_MONTH, lStartDay);"
Stone.lia at 2007-7-12 21:22:20 > top of Java-index,Java Essentials,Java Programming...
# 2

Did it solve the problem ?

I placed

//lCalendar.set(Calendar.DATE, lStartDay);

lCalendar.set(Calendar.DAY_OF_MONTH, lStartDay);

this but it still shows 30th July 2007 if i comment out the line

//lCalendar.getTime();

Not only that if you take any date it shows only 30th July 2007

sukanta123a at 2007-7-12 21:22:20 > top of Java-index,Java Essentials,Java Programming...
# 3

sorry. I cannot learn your means first.

Yes, it does not solve the problem.

That because 'Now' is the last week of the month.when not invoke the getTime method, the calendar object do not calculate the time by the setter;so that, set(Calendar.DAY_OF_WEEK, Calendar.MONDAY) as the last setter affect the calculate of the time.Videlicet, the calendar object get the monday of the week which include the now.

Stone.lia at 2007-7-12 21:22:20 > top of Java-index,Java Essentials,Java Programming...
# 4

> lCalendar.set(Calendar.YEAR,lStartYear);

> lCalendar.set(Calendar.MONTH, lStartMonth);

> lCalendar.set(Calendar.DATE, lStartDay);

> lCalendar.set(Calendar.HOUR_OF_DAY, 0);

> lCalendar.set(Calendar.MINUTE, 0);

> lCalendar.set(Calendar.SECOND,0);

Just an idea, use the 6-argument set method in Calendar? Might change the behaviour?

OleVVa at 2007-7-12 21:22:20 > top of Java-index,Java Essentials,Java Programming...
# 5

Actually it's neater to use the clear() method, saves putting in all those zeros.

The behaviour of Calendar is that you set a series of fields but it can't always calculate a sensible date until several fields have been set. For example you can ask if for, say, the second to last friday in Jun of some year.

So what happens is that it doesn't calculate a date until either the date is requested from it, or you do an add type operation. Without calling getTime() you are effectively setting two, mutually exclusive values, a day of month and a day of week that disagree.

BTW you may need to check the calendar's first day of week, some start their week on Sunday, some on Monday.

malcolmmca at 2007-7-12 21:22:20 > top of Java-index,Java Essentials,Java Programming...
# 6
Yes in this way i tried it but same result found 30th July 2007
sukanta123a at 2007-7-12 21:22:20 > top of Java-index,Java Essentials,Java Programming...
# 7

you may debug the code of the Calendar and find the computeTime() method on the class GregorianCalendar.When computing the time,The GregorianCalendar would adjust the mask on the day.

In the selectFields(), thers is a snippet. Can find "lCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);" affect the compute in nature.

//--

if (bestStamp == domStamp ||

(bestStamp == womStamp && stamp[WEEK_OF_MONTH] >= stamp[WEEK_OF_YEAR]) ||

(bestStamp == dowimStamp && stamp[DAY_OF_WEEK_IN_MONTH] >= stamp[WEEK_OF_YEAR])) {

fieldMask |= MONTH_MASK;

if (bestStamp == domStamp) {

fieldMask |= DAY_OF_MONTH_MASK;

} else {

assert (bestStamp == womStamp || bestStamp == dowimStamp);

if (dowStamp != UNSET) {

fieldMask |= DAY_OF_WEEK_MASK;

}

if (bestStamp == womStamp) {

fieldMask |= WEEK_OF_MONTH_MASK;

} else {

assert (bestStamp == dowimStamp);

if (stamp[DAY_OF_WEEK_IN_MONTH] != UNSET) {

fieldMask |= DAY_OF_WEEK_IN_MONTH_MASK;

}

}

}

}

Stone.lia at 2007-7-12 21:22:20 > top of Java-index,Java Essentials,Java Programming...