Calendar giving unexpected results for the year 1

I tried to make this golf code call in Java 7. Just for those who don’t know: is to complete a specific task in a few bytes. Obviously, Java is not a suitable programming language for this, especially with languages ​​such as Jelly; 05AB1E; Pyth; and similar ones that perform tasks of 1-15 bytes, which will be 75-300 in Java, but I just do it for fun.

Here is my current Java 7 answer. For reference, I will 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 displays all months with 1 index, the last day of which is Monday:

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

, , 1 . - , ? , Calendar.getInstance() , new GregorianCalendar(), . , .

1?

+4
1

, GregorianCalendar . "/ ".

, , , :

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

, , "4" - 12 - " "... < 12, < 11.

+7

Source: https://habr.com/ru/post/1659036/


All Articles