/* daymath.java * * The author places the following code in the public domain. * * Contact: Andy Goth * * For useful information on dates and calendars, visit * */ import java.io.*; public class daymath { /** Returns true if and only iff d/m/y is a valid date. */ public static boolean validate(int d, int m, int y) { /* Month lengths, in days. */ int length[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; /* Months must be in the range [1..12]. */ if (m < 1 || m > 12) { return false; } /* Years must be AD. */ if (y < 1) { return false; } /* Days must be within the month. Allow Feb 29 for leap years. */ if (d < 1 || (d > length[m - 1] && (m != 2 || d != 29 || y % 4 != 0 || (y % 100 == 0 && y % 400 != 0)))) { return false; } /* Valid. */ return true; } /** Returns the number of days between d/m/y and 01/01/2000; positive * indicates d/m/y occurs after 01/01/2000. */ public static int count(int d, int m, int y) { /* Treat January and February as extensions to the previous year, * as in Zeller's congruence. */ if (m < 3) { m += 12; y -= 1; } return -730517 /* Constant gives base date 2000-01-01 AD. */ + d /* Start with the day of the month. */ + ((153 * m - 2) / 5) /* Add the beginning of the month. */ + 365 * y /* Most years are 365 days in length, */ + (y / 4) /* but every fourth year has an extra day, */ - (y / 100) /* except for every hundredth year */ + (y / 400); /* that isn't a multiple of 400. */ } /** Returns the number of days between d1/m1/y1 and d2/m2/y2, inclusive. */ public static int difference(int d1, int m1, int y1, int d2, int m2, int y2) { return Math.abs(count(d1, m1, y1) - count(d2, m2, y2)); } public static int main(String[] args) { int i; BufferedReader in; String line; String part; int dmy[] = new int[6]; /* Prepare a line-oriented input reader. */ in = new BufferedReader(new InputStreamReader(System.in)); /* Main loop: process each line. */ while (true) { /* Wait for and read a line of input (or EOF). */ line = in.readLine(); /* End of file? If so, quit. */ if (line == null) { break; } /* Drop leading and trailing whitespace. */ line = line.trim(); /* Check input format. */ if (!line.matches("^(\\d+\\s*,\\s*){5}\\d+$")) { System.err.println("Bad input format; should be " + "\"d1,m1,y1,d2,m2,y2\""); continue; } /* Pick apart the line into date components. */ i = 0; for (part : line.split("\\s*,\\s*")) { dmy[i] = Integer.getInteger(part); ++i; } /* Check validity of dates. */ for (i = 0; i < 6; i += 3) { if (!validate(dmy[i + 0], dmy[i + 1], dmy[i + 2])) { System.err.printf("Bad date: \"%02d/%02d/%04d\"\n", dmy[i + 0], dmy[i + 1], dmy[i + 2]); break; } } if (i != 6) { continue; } /* Do it! */ System.out.println(difference(dmy[0], dmy[1], dmy[2], dmy[3], dmy[4], dmy[5])); /* Loop around and repeat with the next line. */ } /* Done. */ } } /* vim: set ts=4 sts=4 sw=4 tw=80 et: */