import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
public class CalendarRecycle {
	public static void main(String[] args) {
		//first list: for a given year, show the prior equivalent dates 
		for(int c=2009;c<2020;c++){	//years we want lists for
			System.out.print(c+": ");
			for(int p=c-1;p>=1900;p--){	//years to try
				if(areSame(c,p))
					System.out.print(p+" ");
			}
			System.out.println();
		}
		System.out.println();
		System.out.println();
		System.out.println();
		
		//second list: show rows of equivalent years
		String[] lists=new String[3000];	//array to hold lists of equivalent years
		for(int y1=1900;y1<=2100;y1++){	//populate with theses years
			boolean found=false;	//did we find an equivalent year in the list?
			for(int y2=0;y2<lists.length;y2++){	//go through entire range of possible lists
				if(lists[y2]==null)	//if no list defined for this year
					continue;	//skip
				if(areSame(y1,y2)){	//if the year we are looking is "the same" as an existing list
					lists[y2]+=" "+y1;	//add it to the list
					found=true;	//set flag
					break;	//stop looking
				}
			}
			if(!found)	//if we didnt find year
				lists[y1]=""+y1;	//create new list containing this year
		}
		for(int i=0;i<lists.length;i++)	//go through all possible years
			if(lists[i]!=null)	//if there is a list for this year
				System.out.println(lists[i]);	//print it
	}
	
	//are the two years "the same"? (can use same calendar for both years)
	public static boolean areSame(int year1, int year2){
		Calendar c_b=new GregorianCalendar();	//current year begin date (first day of year)
		Calendar p_b=new GregorianCalendar();	//past year begin date (first day of year)
		Calendar c_e=new GregorianCalendar();	//current year end date (last day of year)
		Calendar p_e=new GregorianCalendar();	//past year end date (last day of year)
		
		//set beginning dates
		c_b.set(Calendar.MONTH, 0);
		c_b.set(Calendar.DAY_OF_MONTH, 1);
		p_b.set(Calendar.MONTH, 0);
		p_b.set(Calendar.DAY_OF_MONTH, 1);
		
		//set end dates
		c_e.set(Calendar.MONTH, 11);
		c_e.set(Calendar.DAY_OF_MONTH, 31);
		p_e.set(Calendar.MONTH, 11);
		p_e.set(Calendar.DAY_OF_MONTH, 31);

		//set years
		c_b.set(Calendar.YEAR, year1);
		c_e.set(Calendar.YEAR, year1);
		p_b.set(Calendar.YEAR, year2);
		p_e.set(Calendar.YEAR, year2);
		
		//looking at the two years,
		//if the first days of the year share the same day of the week
		//and the last days of the year share the same day of the week
		//the calendars are assumed to be equivalent
		if(c_b.get(Calendar.DAY_OF_WEEK)==p_b.get(Calendar.DAY_OF_WEEK) && c_e.get(Calendar.DAY_OF_WEEK)==p_e.get(Calendar.DAY_OF_WEEK))
			return true;
		return false;
		
	}
}

