Calendar giving unexpected results for year 1

I tried to do this code-golf challenge in Java 7. Just for anyone that doesn't know: is to complete a certain task in as few bytes as possible. Obviously Java isn't a suitable programming language to do this in, especially with languages like Jelly; 05AB1E; Pyth; and alike who complete tasks in 1-15 bytes which will be 75-300 in Java, but I just do it for fun.

Here is my current Java 7 answer. Just for reference, I'll also copy it here:

import java.util.*;String c(int y){String r="";Calendar c=Calendar.getInstance();c.set(1,y);c.set(2,0);for(int i=0;i++<11;c.add(2,1)){c.set(5,c.getActualMaximum(5));if(c.get(7)==2)r+=i+" ";}return r;}

import java.util.*;
class M{
  static String c(int year){
    String r = "";
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, 0);
    for(int i = 0; i++ < 11; calendar.add(Calendar.MONTH, 1)){
      calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
      if(calendar.get(Calendar.DAY_OF_WEEK) == 2){
        r += i+" ";
      }
    }
    return r;
  }

  public static void main(String[] a){
    System.out.println(c(1));
    System.out.println(c(297));
    System.out.println(c(1776));
    System.out.println(c(2000));
    System.out.println(c(2016));
    System.out.println(c(3385));
  }
}

Which outputs all the 1-indexed months of which the last day of the month is a Monday:

1 2 10 **
5 
9 
1 7 
2 10 
1 2 10

As you may have note when you compare my results with the results in the linked challenge, the results for year 1 are incorrect.
Does anyone know why? Originally I thought Calendar.getInstance() has the incorrect calendar as default, so I changed it to new GregorianCalendar() as specified in the challenge. But I realized this is the default calendar already.

Why are there incorrect results for year 1?

Jon Skeet
people
quotationmark

Unfortunately, GregorianCalendar is badly named. It's actually "Julian / Gregorian calendar, with a switch-over between them."

Fortunately, you can use it to act as a pure Gregorian calendar by setting that switch-over to be at the start of time:

GregorianCalendar calendar = new GregorianCalendar();
calendar.setGregorianChange(new Date(Long.MIN_VALUE));

At that point, with your current code, you get an answer of just "4" - it's not picking up 12 due to an off-by-one error... you want < 12 rather than < 11.

people

See more on this question at Stackoverflow