Skip to content

Instantly share code, notes, and snippets.

@LightSystem
Created March 27, 2014 13:50
Show Gist options
  • Save LightSystem/9808003 to your computer and use it in GitHub Desktop.
Save LightSystem/9808003 to your computer and use it in GitHub Desktop.
/**
*
* Implements the standard calculation for the final age
*
* @param birthDate
* @param contractBeginDate
* @return the technical age
* @throws InvalidBirthDateException
*/
private int calculateFinalAge(Calendar birthDate, Calendar contractBeginDate) throws InvalidBirthDateException{
int birthYear = birthDate.get(Calendar.YEAR);
int birthMonth = birthDate.get(Calendar.MONTH);
int contractBeginYear = contractBeginDate.get(Calendar.YEAR);
int contractBeginMonth = contractBeginDate.get(Calendar.MONTH);
int tecAge;
if(birthDate.after(contractBeginDate)){
throw new InvalidBirthDateException("birthDate cannot be defined as being after the supplied contractBeginDate");
} else {
int yearDif = contractBeginYear - birthYear;
int monthDif = contractBeginMonth - birthMonth;
if(monthDif < 0){
monthDif += 12;
yearDif -= 1;
}
if(monthDif > 6){
yearDif += 1;
}
tecAge = yearDif;
return tecAge;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment