FileDocCategorySizeDatePackage
Age.javaAPI DocExample3176Tue Nov 04 19:11:30 GMT 1997None

Age

public class Age extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

  // Get three values for:
      int day = 0;
      int month = 0;
      int year = 0;

      String line = null;
      BufferedReader is = new BufferedReader( new InputStreamReader( System.in ) );

      System.out.print( "Enter birthday ( day month year ) : " );
      while( true )
      {  try
         {  line = is.readLine();
            StringTokenizer st = new StringTokenizer( line );
            String item = st.nextToken();
            day = new Integer( item.trim() ).intValue();
            item = st.nextToken();
            month = new Integer( item.trim() ).intValue() - 1; // month !!
            item = st.nextToken();
            year = new Integer( item.trim() ).intValue();
            if( year < 100 ) // this century
            {  year += 1900;
            }
            break;
         }
         catch( IOException ioe )
         {  // real bother
            System.exit( -1 );
         }
         catch( Exception e ) // others
         {  System.out.println( "Illegal format - do again" );
         }
      }
      // Assert: day, month, year are all set to int values but they do
      //         not neccesarily consitute a vaild date!

      // Set up a calendar object for the birthday
      Calendar birthday = new GregorianCalendar( year, month, day, 0, 0, 0 );
                                                // hr, min, sec = 0

      // For output we need a formatter
      // The default one for our locale first:
      SimpleDateFormat format1 = new SimpleDateFormat();
      // and one set to Day name, date, Month name and year in full
      SimpleDateFormat format2 = new SimpleDateFormat( "EEEE d MMMM yyyy" );

      // output using the formatters
      // Note: the calendar returns date and time together as a Date object
      System.out.println( "You were born on : " + format1.format( birthday.getTime() ) );
      System.out.println( "or : " + format2.format( birthday.getTime() ) );

      // Set up a calendar for the current date and time
      Calendar now = new GregorianCalendar();

      // Now to work out the age in years
      int ageInYears = now.get( Calendar.YEAR ) - birthday.get( Calendar.YEAR );

      // and adjust according exactly when the birthday falls
      // Calendar.DAY_OF_YEAR looks useful but leap years complicate its use, so...
      if( now.get( Calendar.MONTH ) < birthday.get( Calendar.MONTH ) )
      {  ageInYears -= 1;
      }
      else if( (now.get( Calendar.MONTH ) == birthday.get( Calendar.MONTH ))
               & (now.get( Calendar.DAY_OF_MONTH ) < birthday.get( Calendar.DAY_OF_MONTH )) )
      {  ageInYears -= 1;
      }
      else if( (now.get( Calendar.MONTH ) == birthday.get( Calendar.MONTH ))
               & (now.get( Calendar.DAY_OF_MONTH ) == birthday.get( Calendar.DAY_OF_MONTH )) )              
      {  System.out.print( "Happy Birthday! " );
      }
      
      System.out.println( "You are " + ageInYears + " years old." );

      // Phew!