Calendar.WEEK_OF_YEAR is not working correctly
Calendar.WEEK_OF_YEAR is not working correctly. My goal is every Monday is a new week. The results I am getting are in the code comments.
But I cannot find a solution.
My code:
Calendar cal = Calendar.getInstance();
//cal.setMinimalDaysInFirstWeek(7);
//cal.setFirstDayOfWeek(0);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date d = sdf.parse("27/12/2014"); //SAT
Date d1 = sdf.parse("28/12/2014"); //SUN
cal.setTime(d);
Integer weekNumber1 = cal.get(Calendar.WEEK_OF_YEAR);
cal.setTime(d1);
Integer weekNumber2 = cal.get(Calendar.WEEK_OF_YEAR);
System.out.println( weekNumber1 );// 52
System.out.println( weekNumber2 );// 1`
second:
Calendar cal = Calendar.getInstance();
cal.setMinimalDaysInFirstWeek(7);
//cal.setFirstDayOfWeek(0);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date d = sdf.parse("27/12/2014"); //SAT
Date d1 = sdf.parse("28/12/2014"); //SUN
cal.setTime(d);
Integer weekNumber1 = cal.get(Calendar.WEEK_OF_YEAR);
cal.setTime(d1);
Integer weekNumber2 = cal.get(Calendar.WEEK_OF_YEAR);
System.out.println( weekNumber1 );// 51
System.out.println( weekNumber2 );// 52
And third:
Calendar cal = Calendar.getInstance();
cal.setMinimalDaysInFirstWeek(7);
cal.setFirstDayOfWeek(0);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date d = sdf.parse("27/12/2014"); //SAT
Date d1 = sdf.parse("28/12/2014"); //SUN
Date d2 = sdf.parse("29/12/2014"); //Mon
cal.setTime(d);
Integer weekNumber1 = cal.get(Calendar.WEEK_OF_YEAR);
cal.setTime(d1);
Integer weekNumber2 = cal.get(Calendar.WEEK_OF_YEAR);
cal.setTime(d2);
Integer weekNumber3 = cal.get(Calendar.WEEK_OF_YEAR);
System.out.println( weekNumber1 );// 52
System.out.println( weekNumber2 );// 52
System.out.println( weekNumber3 );// 52
+3
source to share