/* Date Theory #1 - Repeating Calendar Written by: Keith Fenske Sunday, 18 December 2005 Java class name: DateTheory1 Copyright (c) 2005 by Keith Fenske. All rights reserved. This is a Java 1.4 console application to find the minimum interval when the calendar repeats for every day of the year. Testing is limited to a 99-year span starting in 2001. The result is an interval of 28 years, with "normal" leap years only (multiples of four, not 100, not 400, etc). This program is the property of Keith Fenske. */ import java.io.*; // standard I/O import java.util.*; // dates public class DateTheory1 { /* constants */ static final int maxInterval = 99; // maximum number of years to check static final int startYear = 2001; // start checking from this year /* main() method This application only has one main method. There is no graphical interface. */ public static void main(String[] args) { int day; // current day of the month boolean found; // true if everything is matching so far int interval; // interval being tested in years int month; // current month of the year Calendar myCalendar; // for converting year-month-day to weekday int weekday; // calculated day of the week int year; // current year /* Initialize a Java Calendar object so that we can convert dates into days of the week. */ myCalendar = Calendar.getInstance(); // use same calendar for all dates myCalendar.clear(); // clear fields we don't use (time, etc) /* This nested sequence of loops checks every possible combination. Since Java is lenient about dates being out of range (i.e., using a day 31 in February), we blindly loop through all numeric dates. If we were clever, we would only check two special days in each year, say, January 1st and March 1st. If those match, then all other days in the same years will match. However, since this is more of a programming exercise than a need for speed, no attempt is made to optimize the search. Note the statements and flag used to escape early from the loops. */ found = false; // keep compiler happy by assuming failure for (interval = 1; interval <= maxInterval; interval ++) { System.out.println("Testing interval of " + interval + " years."); found = true; // start by assuming that interval matches for (year = startYear; year < (startYear + interval); year ++) { for (month = Calendar.JANUARY; month <= Calendar.DECEMBER; month ++) { for (day = 1; day <= 31; day ++) { myCalendar.set(year, month, day); // get weekday for interval start weekday = myCalendar.get(Calendar.DAY_OF_WEEK); myCalendar.set(year + interval, month, day); // for interval end if (weekday != myCalendar.get(Calendar.DAY_OF_WEEK)) { found = false; // calendar weekdays don't match break; // stop looking for more days } } // end of for-day loop if (!found) // was there a mismatch in day loop? break; // yes, stop looking for more months } // end of for-month loop if (!found) // was there a mismatch in month loop? break; // yes, stop looking for more years } // end of for-year loop if (found) // did everything match in all loops? { System.out.println("Calendar repeats after " + interval + " years."); System.out.println("Years from " + startYear + " to " + (startYear + interval - 1) + " are identical to years from " + (startYear + interval) + " to " + (startYear + (2 * interval) - 1) + "."); break; // stop looking for more intervals } } // end of for-interval loop if (!found) // did we find a good interval? { System.out.println( "Unable to find a repeating calendar after looking for " + maxInterval + " years."); } } // end of main() method } // end of DateTheory1 class /* Copyright (c) 2005 by Keith Fenske. All rights reserved. */