RSS feed [root] /weblog /java /hacks



title search:


Fri Apr 07 05:13:55 GMT 2006


I don't know this before, and I don't know if this is the behaviour of all Calendar implementation or it is only GregorianCalendar do this...

Every time you call set, Calendar sets a flag internally which indicates that something has changed. For performance reasons, the time is not actually computed until a call to getTime() (and variants).

The problem is that you are setting two conflicting states before computing the time:

thisMonth.set(Calendar.DAY_OF_MONTH, 1);
thisMonth.set(Calendar.DAY_OF_WEEK, 1);

According to the JavaDoc:
"If fields conflict, the calendar will give preference to fields set more recently."

Thus, in the second case, the call to set DAY_OF_MONTH is ignored.

(google search) (amazon search)
download zip of files only