Is this a bug in Android GregorianCalendar?

So this is January 26th, 2015, and I have the following code running on an Android device,

GregorianCalendar date = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat();

// Set date to 4 weeks ago, and then use it for the first BETWEEN date in the above query    
date.set(GregorianCalendar.WEEK_OF_YEAR, date.get(GregorianCalendar.WEEK_OF_YEAR)-4);
System.out.println(df.format(new Date(date.getTimeInMillis())));
// Set date to 2 weeks time, and then use it for the second BETWEEN date in the above query

date.set(GregorianCalendar.WEEK_OF_YEAR, date.get(GregorianCalendar.WEEK_OF_YEAR)+6);
System.out.println(df.format(new Date(date.getTimeInMillis())));

And I get the output:

29/12/14 10:29
09/02/14 10:29

Running this snippet on standard Java on a Windows computer shows the correct results with the second date as 09/02/15 10:29. So, on Android, we will go back when we ask the date to go back 4 weeks, but when we ask it to go forward 6 weeks (after our original date), it does not take the year forward again.

I watched this on 5.0.2 and 4.4.2

So the question is, is this a mistake or some kind of confusing supposed behavior (function)?

+4
source share
2

, , Calendar, . , , add(...), , .

...

, getFirstDayOfWeek() getMinimalDaysInFirstWeek() .

, .. WEEK_OF_YEAR = 1, 4- 5- , .

Calendar , , , -, 0 -1 . , 1 → 3 1 → 4 ( ), , 2015 , , , 28 29 , 0.

26 WEEK_OF_YEAR = 4 ( ), , , 4 WEEK_OF_YEAR, 0 , 29- .

, 6 - 29 2014 . , , add(...). , , 2014 6 , / 2014 , , YEAR, WEEK_OF_YEAR ..

+2

Calendar.set . , . JVM, , , , Android -.

Calendar.add , , : .

date.add(GregorianCalendar.WEEK_OF_YEAR, -4);
date.add(GregorianCalendar.WEEK_OF_YEAR, 6);
+2

All Articles