how to calculate months between two dates in java code example

Example 1: java get number of months between two dates

LocalDate currentDay = LocalDate.of(1955, Month.MAY, 19);
		LocalDate desiredDay = LocalDate.now();
		Period age = Period.between(currentDay, desiredDay);
		int years = age.getYears();
		int months = age.getMonths();

		int numberOfMonthsBetweenDates =  months+years*12;

Example 2: how to calculate the amount of months between two dates java

LocalDate jamesBirthDay = new LocalDate(1955, 5, 19);
LocalDate now = new LocalDate(2015, 7, 30);
        
int monthsBetween = Months.monthsBetween(jamesBirthDay, now).getMonths();
int yearsBetween = Years.yearsBetween(jamesBirthDay, now).getYears();
        
System.out.println("Month since father of Java born : " 
                    + monthsBetween);
System.out.println("Number of years since father of Java born : " 
                    + yearsBetween);

Tags:

Java Example