// file: testDate.java // author: Robert Keller // purpose: demonstrating Date class // operation: The user types dates consisting of a month, day, and year // The program indicates the age of a person born on that date. // The month is a number from 1 to 12, the day from 1 to 31, // and the number of the year since 1900. import java.util.Date; import java.io.*; class testDate { static final long msPerYear = 31536000000L; static PrintStream out = System.out; static boolean prompt() { out.print("Please enter month day and year since 1900, e.g. 2 16 98: "); out.flush(); return true; } static public void main(String arg[]) { out.println("testDate returns age in years of person born on date entered."); out.println(); StreamTokenizer in = new StreamTokenizer(System.in); while( prompt() ) { try { if( in.nextToken() == StreamTokenizer.TT_EOF ) break; int month = (int)in.nval - 1; // months go from 0 to 11 if( in.nextToken() == StreamTokenizer.TT_EOF ) break; int day = (int)in.nval; if( in.nextToken() == StreamTokenizer.TT_EOF ) break; int year = (int)in.nval; // years int correction = year < 70 ? 70-year : 0; // year of Date must be >= 70 Date date = new Date(year + correction, month, day); long ms = System.currentTimeMillis() - date.getTime(); out.println(ms/msPerYear + correction); } catch( IOException e ) { System.err.println("IOException caught"); System.exit(1); } } out.println(); } }