package de.ivu.elect.business.person.control;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *  on 08.08.2016.
 */
public class AlterBerechnen {

  public static int calculateAge(Date from, Date to) {
    int age = 0;

    if (to.after(from)) {
      Calendar birthday = new GregorianCalendar();
      birthday.setTime(from);

      Calendar someDay = new GregorianCalendar();
      someDay.setTime(to);

      int diffYears = someDay.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
      int diffMonths = someDay.get(Calendar.MONTH) - birthday.get(Calendar.MONTH);
      int diffDaysOfMonth = someDay.get(Calendar.DAY_OF_MONTH) - birthday.get(Calendar.DAY_OF_MONTH);

      age = diffYears;

      if (diffMonths < 0 || diffMonths == 0 && diffDaysOfMonth < 0) {
        age--;
      }
    }

    return age;
  }

}
