public int getAgeDifference(Date date) {
int ageDifference;
Calendar theirBirthday = new GregorianCalendar();
Calendar dukesBirthday = new GregorianCalendar(1995, Calendar.MAY, 23);
// Set the Calendar object to the passed-in Date
theirBirthday.setTime(date);
// Subtract the user's age from Duke's age
ageDifference = dukesBirthday.get(Calendar.YEAR)
- theirBirthday.get(Calendar.YEAR);
logger.log(Level.INFO, "Raw ageDifference is: {0}", ageDifference);
// Check to see if Duke's birthday occurs before the user's. If so,
// subtract one from the age difference
if (dukesBirthday.before(theirBirthday) && (ageDifference> 0)) {
ageDifference--;
}
// Create and store the user's birthday in the database
FirstcupUser user = new FirstcupUser(date, ageDifference);
em.persist(user);
logger.log(Level.INFO, "Final ageDifference is: {0}", ageDifference);
return ageDifference;
}