package username.dates; /** * A variety of utilities to aid in the manipulation of dates. * * @author Samuel A. Rebelsky * @version 1.0 of September 2005 */ public class DateHelper { // +---------------+----------------------------------------------------- // | Static Fields | // +---------------+ /** * An array such that months[i] is the name of the ith month. */ static String[] months = new String[] { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; // months /** * The number of days in each month. */ static int[] daysInMonth = new int[] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // daysInMonth // +----------------+---------------------------------------------------- // | Static Methods | // +----------------+ /** * Get the name of a month. */ public static String nameOfMonth(int month) { return months[month]; } // nameOfMonth(int) /** * Get the number of days in a month. Returns 0 for month 0. */ public static int daysInMonth(int month) { return daysInMonth[month]; } // daysInMonth(int) /** * Get the number of a month. Returns 0 for an unknown name. */ public static int monthNum(String name) { for (int i = 1; i <= 12; i++) { if (months[i].equals(name)) { return i; } // if } // for // Whoops. We failed. return 0; } // monthNum(String) /** * Determine the number of days between two dates. */ public static int daysBetween(SimpleDate d1, SimpleDate d2) { int guess1 = Math.abs(d1.dayOfYear() - d2.dayOfYear()); int guess2 = 365-guess1; if (guess2 < guess1) { return guess2; } else { return guess1; } } // daysBetween(Date, Date) } // class DateHelper