// NowKL.java Copyright (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-06-22 File created. // 2005-06-22 Last modification. // A solution to Exercise 12-2. import java.util.* ; class NowKL { public static void main( String[] not_in_use ) { Date date_of_birth = new Date( "30.12.1988" ) ; Date date_to_increment = new Date( date_of_birth.toString() ) ; // Now date_of_birth and date_to_increment refer to identical, // but different, objects. CurrentDate date_today = new CurrentDate() ; int day_counter = 0 ; while ( date_to_increment.is_earlier_than( date_today ) ) { date_to_increment.increment() ; day_counter ++ ; } System.out.print( "\n\n Today " + date_to_increment + " you are " + day_counter + " days old. " ) ; int years_of_age, months_of_age, days_of_age ; DateDistance current_age = date_today.get_distance_to( date_of_birth ) ; System.out.print( "\n That is " + current_age.years + " years, " + current_age.months + " months, and " + current_age.days + " days. \n " ) ; // At this phase of the execution of this program // date_to_increment references a Date object that // contains the date of today. We'll continue incrementing // it until the next birthday. int number_of_days_to_next_birthday = 0 ; while ( date_to_increment.day() != date_of_birth.day() || date_to_increment.month() != date_of_birth.month() ) { date_to_increment.increment() ; number_of_days_to_next_birthday ++ ; } System.out.print( "\n There are " + number_of_days_to_next_birthday + " days until your next birthday. \n" ) ; } }