Let’s look at the following code snippet in Java.
package demo;
import java.util.Calendar;
final public class Main
{
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
cal.set(2011, 11, 11); //Setting a date to itself.
System.out.println(cal.get(Calendar.YEAR) + " "
+ cal.get(Calendar.WEEK_OF_YEAR) + "
" + cal.get(Calendar.DAY_OF_YEAR));
}
}
In the above simple code, I’m temporarily (and explicitly) setting the current date to itself which is 2011-11-11 using the method cal.set(2011, 11, 11);. Accordingly, I get the output 2011 51 345, the current year 2011, the week of the year 51 and the day of the year 345.
When I leave a comment on that line which is cal.set(2011, 11, 11);, I get the result, 2011 46 315 the current year, the current week of the year and the current day of the year respectively which is different from the earlier result.
[Here, I’m not setting any date. The statement cal.set(2011, 11, 11); in the above code is commented out and the system is automatically retrieving the current date which is 2011-11-11].
In both the cases, the same date is used which is 2011-11-11 still the result obtained is different in both of the cases. Why?
The month value is 0-based. If you meant November, use:
See the Calendar javadocs.