How to calculate age in year and month between two dates in Java

I am a newbie and appreciate if anyone helps me.

When I tried to calculate age using the source below, it does not give me the value of what I want. For example: date-> 29/12/2010, dob-> 30/12/1992, this will give me 18 instead of 17. Is there any method that I can program to return me 17yrs 11mths based on the above 2 dates instead 18yrs0mths?

public double getAgeAsOf( Date date ) { return ((date.getTime() - dob.getTime())/(1000*60*60*24))/365; } 

Thanks.

+4
source share
2 answers

You can use Joda Time and calculate Period between the two LocalDate values ​​(which you got here), using months and years as units.

Code example:

 import org.joda.time.*; public class Test { public static void main(String[] args) { LocalDate dob = new LocalDate(1992, 12, 30); LocalDate date = new LocalDate(2010, 12, 29); Period period = new Period(dob, date, PeriodType.yearMonthDay()); System.out.println(period.getYears() + " years and " + period.getMonths() + " months"); } } 

(This uses a period type that also includes days, but this will not affect the response.)

All in all, Joda Time is a much better API than using Date / Calendar - and you really don't want you to do the date calculations yourself if you can avoid it. It gets very dirty.

According to the answer in aioobe, if you separate the two integer expressions, the arithmetic will be done in integer arithmetic, which may not be what you want, but for date and time arithmetic, just let someone else do the hard work first: )

The code above will use the ISO-8601 calendar, which is basically a Gregorian calendar. If you want to use something else, specify it as another constructor argument after the year / month / day for LocalDate .

+16
source

You have a few problems with the code:

  • You do integer division, which truncates the result to the nearest lower integer.

    For example, if you divide 729 by 365, you will get 1 (and you have lost the fractional part that you will need when calculating the months, etc.)

  • Another problem is that you use 365 days for one year, while in reality it is closer to 365.25 (when you include extra days due to leap years).

Here's a slightly improved code snippet:

 import java.util.Date; public class Test { static double msPerGregorianYear = 365.25 * 86400 * 1000; static Date dob = new Date(1992, 12, 30); public static double getAgeAsOf(Date date) { return (date.getTime() - dob.getTime()) / msPerGregorianYear; } public static void main(String[] args) { double years = getAgeAsOf(new Date(2010, 12, 29)); // years == 17.99315537303217 int yy = (int) years; int mm = (int) ((years - yy) * 12); // Prints "17 years and 11 moths." System.out.printf("%d years and %d moths.", yy, mm); } } 

If you ever do something more complicated than calculating the number of years, given the number of milliseconds, I suggest you go to some temporary library, like Joda's Time , as suggested by John Skeet.

+2
source

Source: https://habr.com/ru/post/1344608/


All Articles