Since it seems like people are more concerned about voting down than reading the actual question, the values I should be getting are in the bottom box, and my question is right under it
I need to find the age of a child based on a user entered birth date and today's date. The user has the option of viewing the age in years, months, weeks, and days. I need to make sure that the calculations account for leap years. I have gone through several posts that suggest different tricks, but so far I haven't found anything that calculates reliable numbers. The closest I've come to a solution is:
final Calendar bday = Calendar.getInstance();
bday.set(1991, 3, 27);
final Calendar today = Calendar.getInstance();
int yearsDiff = today.get(Calendar.YEAR) - bday.get(Calendar.YEAR); // yearsDiff: 24
if(bday.get(Calendar.MONTH) > today.get(Calendar.MONTH) ||
(bday.get(Calendar.MONTH) == today.get(Calendar.MONTH) && bday.get(Calendar.DATE) > today.get(Calendar.DATE))) {
yearsDiff --;
}
final int monthsDiff = yearsDiff * 12 + today.get(Calendar.MONTH) - bday.get(Calendar.MONTH); // monthsDiff: 296
final long timeOne = bday.getTime().getTime();
final long timeTwo = today.getTime().getTime();
final long oneDay = 1000 * 60 * 60 * 24;
final long delta = (timeTwo - timeOne) / oneDay;
final long year = delta / 365; // year: 24
final long rest = delta % 365; // rest: 232 delta: 8992
final long month = rest / 30; // month: 7
final long rest2 = rest % 30; // rest2: 22 rest: 232
final long weeks = rest2 / 7; // weeks: 3
final long days = rest2 % 7; // days: 1 rest2: 22
yearsDiff and monthsDiff are both correct. Correct values (as of today, 12-09-2015) are:
years: 24
months: 296
weeks: 1289
days: 9023
You'll notice that there are two different I'm calculating some of these values. That's because I'm looking through different answers and trying to find the best solution.
How can I find the correct count of weeks and days?