import java.io.*;
import java.util.*;
public class ComputeAge1 {
public static void main(String args[])throws IOException {
String inString;
int bYear = 0, bMonth = 0, bDay = 0;
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(is);
boolean valid=false;
// V.V.
while (!valid) {
try {
System.out.println("Please enter the year of your birth:");
inString = stdin.readLine();
bYear = Integer.parseInt(inString);
System.out.println("Please enter the month of your birth:");
inString = stdin.readLine();
bMonth = Integer.parseInt(inString);
System.out.println("Please enter the Day of your birth:");
inString = stdin.readLine();
bDay = Integer.parseInt(inString);
if (bYear<1800) System.out.println("Year must be >=1800");
else {
GregorianCalendar toDay = new GregorianCalendar();
GregorianCalendar birthDay = new GregorianCalendar(bYear, bMonth-1, bDay);
// month parameter is based on 0 to 11 -- V.V.
int birthYear = birthDay.get(Calendar.YEAR);
int birthMonth = birthDay.get(Calendar.MONTH);
int birthDDay = birthDay.get(Calendar.DATE);
boolean inputError=false; // check for other bad inputs
if (birthYear!=bYear) inputError=true;
if (birthMonth+1!=bMonth) inputError=true;
if (birthDDay!=bDay) inputError=true;
if (inputError) System.out.println("You've entered an incorrect date");
else {
System.out.println("You are born in year " + birthYear);
System.out.println("You are born in the month " + (birthMonth+1));
System.out.println("You are born on the day " + birthDDay);
int toDayYear = toDay.get(Calendar.YEAR);
int toDayMonth = toDay.get(Calendar.MONTH);
int toDayDay = toDay.get(Calendar.DATE);
int yearAge = toDayYear - birthYear;
int monthAge = 0;
if (toDayMonth >= birthMonth) { // V.V.
monthAge = toDayMonth - birthMonth;
} else {
yearAge--;
monthAge = 12 +toDayMonth - birthMonth;
}
int dayAge = toDayDay - birthDDay ;
if (toDayDay >= birthDDay) { // V.V.
dayAge = toDayDay - birthDDay;
} else {
monthAge--;
toDay.add(Calendar.MONTH,-1); // V.V.
dayAge = toDay.getActualMaximum(Calendar.DAY_OF_MONTH)+ toDayDay - birthDDay; // V.V.
}
printAgeData(yearAge, monthAge, dayAge);
//call to the method that print age data
valid=true;
}
}
} catch (NumberFormatException exception) { // V.V.
System.out.println("Non-numeric data entered"); // V.V.
}
}
} //main
public static void printAgeData(int yAge, int mAge, int dAge) {
StringBuffer year = new StringBuffer(" year");
StringBuffer month = new StringBuffer(" month");
StringBuffer day = new StringBuffer(" day");
if (yAge > 1)year.append("s"); // V.V.
if (mAge > 1)month.append("s"); // V.V.
if (dAge > 1)day.append("s"); // V.V.
System.out.println("Your age is : " + yAge + year);
System.out.println("Your age is : " + mAge + month);
System.out.println("Your age is : " + dAge + day);
} //printAgeData
} //class