package username.dates; import java.io.PrintWriter; import java.io.InputStreamReader; import java.io.BufferedReader; /** * Some simple tests of dates. * * @author Samuel A. Rebelsky * @version 1.1 of February 2006 */ public class DateTester { public static void main(String[] args) throws Exception { PrintWriter pen = new PrintWriter(System.out, true); BufferedReader eyes = new BufferedReader(new InputStreamReader(System.in)); // Print basic information about the month. for (int i = 1; i <= 12; i++) { pen.println(DateHelper.nameOfMonth(i) + " has " + DateHelper.daysInMonth(i) + " days."); } // for pen.println(); // Read in a birthday. int month; do { pen.print("Please enter the month of your birthday: "); pen.flush(); month = DateHelper.monthNum(eyes.readLine()); if (month == 0) { pen.println("Sorry, I didn't understand that."); } } while (month == 0); int day = 0; do { pen.print("Please enter the day of your birthday: "); pen.flush(); try { day = Integer.parseInt(eyes.readLine()); if ((day < 1) || (day > DateHelper.daysInMonth(month))) { pen.println("Please enter a date between 1 and " + DateHelper.daysInMonth(month)); day = 0; } // if the day is invalid } // try catch (Exception e) { pen.println("That wasn't a numeric date."); day = 0; } } while (day == 0); MonthDay birthday = new MonthDay(month, day); pen.println(); // Explain where in the year the birthday falls. pen.println("Did you know that " + birthday + " falls on day " + birthday.dayOfYear() + " of the year?"); pen.println(); // Figure out which holiday is closer. MonthDay xmas = new MonthDay(12,25); MonthDay independence = new MonthDay(7,4); int deltaxmas = DateHelper.daysBetween(xmas,birthday); int deltaindependence = DateHelper.daysBetween(independence,birthday); if (deltaxmas < deltaindependence) { pen.println(birthday + " is closer to " + xmas + " than to " + independence); } else if (deltaxmas > deltaindependence) { pen.println(birthday + " is closer to " + independence + " than to " + xmas); } else { pen.println(birthday + " is equidistant from " + independence + " and " + xmas); } } // main(String[]) } // class DateTester