### TECHNOLOGY

- Dokuwiki
- Miscellaneous

- Dokuwiki
- Miscellaneous

technology:datetools

This is a set of utility routines to provide guidance to visual calendar building tools. Written in 2001, this class provides mathematical information to calendar systems.

/** * Insert the type's description here. * Creation date: (9/26/01 11:57:50 AM) * @author: cfreyer */ import java.util.Date; import java.util.Calendar; public class DateTools { public static String days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; public static String months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; public static int daysPerMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /** * Insert the method's description here. * Creation date: (9/26/01 2:19:17 PM) * @param userYear int * @param userMonth int */ public static int CalcEndOfMonth(int userYear, int userMonth) { int daysPerMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; return daysPerMonth[userMonth] + ((IsLeapYear(userYear) && (userMonth == Calendar.FEBRUARY)) ? 1 : 0); } public static int CalcFirstOfMonth(int year, int month) /* USE: Calculates day of the week the first day of the month falls on. IN: year = given year after 1582 (start of the Gregorian calendar). month = 0 for January, 1 for February, etc. OUT: First day of month: 0 = Sunday, 1 = Monday, etc. */ { int daysAMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int firstDay; /* day of week for Jan 1, then first day of month */ int i; /* to traverse months before given month */ /* Start at 1582, when modern calendar starts. */ if (year < 1582) return (-1); /* Catch month out of range. */ if ((month < 0) || (month > 11)) return (-1); /* Get day of week for Jan 1 of given year. */ firstDay = CalcJanuaryFirst(year); /* Increase firstDay by days in year before given month to get first day * of month. */ for (i = 0; i < month; i++) firstDay += daysAMonth[i]; /* Increase by one if month after February and leap year. */ if ((month > Calendar.FEBRUARY) && IsLeapYear(year)) firstDay++; /* Convert to day of the week and return. */ return (firstDay % 7); } // CalcFirstOfMonth public static int CalcJanuaryFirst(int year) /* USE: Calculate day of the week on which January 1 falls for given year. IN: year = given year after 1582 (start of the Gregorian calendar). OUT: Day of week for January 1: 0 = Sunday, 1 = Monday, etc. NOTE: Formula starts with a 5, since January 1, 1582 was a Friday; then advances the day of the week by one for every year, adding the number of leap years intervening, because those years Jan 1 advanced by two days. Calculate mod 7 to get the day of the week. */ { /* Start at 1582, when modern calendar starts. */ if (year < 1582) return (-1); /* Start Fri 01-01-1582; advance a day for each year, 2 for leap yrs. */ return ((5 + (year - 1582) + CalcLeapYears(year)) % 7); } // CalcJanuaryFirst public static int CalcLeapYears(int year) /* USE: Calculate number of leap years since 1582. IN: year = given year after 1582 (start of the Gregorian calendar). OUT: number of leap years since the given year, -1 if year < 1582 NOTE: Count doesn't include the given year if it is a leap year. In the Gregorian calendar, used since 1582, every fourth year is a leap year, except for years that are a multiple of a hundred, but not a multiple of 400, which are no longer leap years. Years that are a multiple of 400 are still leap years: 1700, 1800, 1990 were not leap years, but 2000 will be. */ { int leapYears; /* number of leap years to return */ int hundreds; /* number of years multiple of a hundred */ int fourHundreds; /* number of years multiple of four hundred */ /* Start at 1582, when modern calendar starts. */ if (year < 1582) return (-1); /* Calculate number of years in interval that are a multiple of 4. */ leapYears = (year - 1581) / 4; /* Calculate number of years in interval that are a multiple of 100; * subtract, since they are not leap years. */ hundreds = (year - 1501) / 100; leapYears -= hundreds; /* Calculate number of years in interval that are a multiple of 400; * add back in, since they are still leap years. */ fourHundreds = (year - 1201) / 400; leapYears += fourHundreds; return (leapYears); } // CalcLeapYears /** * Insert the method's description here. * Creation date: (9/26/01 2:19:17 PM) * @param userYear int * @param userMonth int */ public static void DaysInMonth(int userYear, int userMonth) { int numDays = daysPerMonth[userMonth] + ((IsLeapYear(userYear) && (userMonth == Calendar.FEBRUARY)) ? 1 : 0); } public static boolean IsLeapYear(int year) /* USE: Determines if given year is a leap year. IN: year = given year after 1582 (start of the Gregorian calendar). OUT: TRUE if given year is leap year, FALSE if not. NOTE: Formulas capture definition of leap years; cf CalcLeapYears(). */ { /* If multiple of 100, leap year iff multiple of 400. */ if ((year % 100) == 0) return ((year % 400) == 0); /* Otherwise leap year iff multiple of 4. */ return ((year % 4) == 0); } // IsLeapYear public static int NumberRowsNeeded(int year, int month) /* USE: Calculates number of rows needed for calendar. IN: year = given year after 1582 (start of the Gregorian calendar). month = 0 for January, 1 for February, etc. OUT: Number of rows: 5 or 6, except for a 28 day February with the first of the month on Sunday, requiring only four rows. */ { int firstDay; /* day of week for first day of month */ int numCells; /* number of cells needed by the month */ int daysPerMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /* Start at 1582, when modern calendar starts. */ if (year < 1582) return (-1); /* Catch month out of range. */ if ((month < 0) || (month > 11)) return (-1); /* Get first day of month. */ firstDay = CalcFirstOfMonth(year, month); /* Non leap year February with 1st on Sunday: 4 rows. */ if ((month == Calendar.FEBRUARY) && (firstDay == 0) && !IsLeapYear(year)) return (4); /* Number of cells needed = blanks on 1st row + days in month. */ numCells = firstDay + daysPerMonth[month]; /* One more cell needed for the Feb 29th in leap year. */ if ((month == Calendar.FEBRUARY) && (IsLeapYear(year))) numCells++; /* 35 cells or less is 5 rows; more is 6. */ return ((numCells <= 35) ? 5 : 6); } // NumberRowsNeeded }

/home/cfreyer/public_html/data/pages/technology/datetools.txt · Last modified: 2008/03/17 01:06 by Chris Freyer