Reputation: 56
I have two dates (01/01/2012, 31/07/2014).
Would you please help me to calculate month difference between this two dates. If difference is 8 months 1 days i need result 9 months.
Upvotes: 2
Views: 17347
Reputation: 41
int months = 12*(toYear-fromYear)+(toMonth-fromMonth)+(toDay>=fromDay?1:0)
Where to is the later date and from is prior date
Upvotes: 1
Reputation: 507
My code:
int getMonthCountBetween(Calendar cal1, Calendar cal2) {
if(cal1.compareTo(cal2)>0) {
Calendar cal=cal1;
cal1=cal2;
cal2=cal;
}
int months=0;
int y1 = cal1.get(Calendar.YEAR);
int y2 = cal2.get(Calendar.YEAR);
int m1 = cal1.get(Calendar.MONTH);
int m2 = cal2.get(Calendar.MONTH);
if (y2 - y1 >= 0) {
months = m2 - m1 + 1;
}
if (y2 - y1 >= 1) {
months += 12;
}
if (y2 - y1 >= 2) {
months += 12 * (y2 - y1 - 1);
}
return months;
}
Upvotes: 0
Reputation: 201537
You might use a Calendar
like,
static int monthsBetween(Date a, Date b) {
Calendar cal = Calendar.getInstance();
if (a.before(b)) {
cal.setTime(a);
} else {
cal.setTime(b);
b = a;
}
int c = 0;
while (cal.getTime().before(b)) {
cal.add(Calendar.MONTH, 1);
c++;
}
return c - 1;
}
then you could call it like
public static void main(String[] args) {
String start = "01/01/2012";
String end = "31/07/2014";
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
System.out.println(monthsBetween(sdf.parse(start), sdf.parse(end)));
} catch (ParseException e) {
e.printStackTrace();
}
}
Output is
30
Or, using Joda-Time
String start = "01/01/2012";
String end = "31/07/2014";
DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try {
LocalDate a = LocalDate.fromDateFields(sdf.parse(start));
LocalDate b = LocalDate.fromDateFields(sdf.parse(end));
Period p = new Period(a, b);
System.out.println((p.getYears() * 12) + p.getMonths());
} catch (ParseException e) {
e.printStackTrace();
}
Output is also
30
Edit
Finally (as suggested in the comments), if you're using Java 8 you might use the new java.time
classes like
String start = "01/01/2012";
String end = "31/07/2014";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate from = LocalDate.parse(start, formatter);
LocalDate to = LocalDate.parse(end, formatter);
System.out.println(from.until(to, ChronoUnit.MONTHS));
Output is (still)
30
Upvotes: 9
Reputation: 1905
Working Code:
public int monthsBetweenDates(Date startDate, Date endDate){
Calendar start = Calendar.getInstance();
start.setTime(startDate);
Calendar end = Calendar.getInstance();
end.setTime(endDate);
int monthsBetween = 0;
int dateDiff = end.get(Calendar.DAY_OF_MONTH)-start.get(Calendar.DAY_OF_MONTH);
if(dateDiff<0) {
int borrrow = end.getActualMaximum(Calendar.DAY_OF_MONTH);
dateDiff = (end.get(Calendar.DAY_OF_MONTH)+borrrow)-start.get(Calendar.DAY_OF_MONTH);
monthsBetween--;
if(dateDiff>0) {
monthsBetween++;
}
}
else {
monthsBetween++;
}
monthsBetween += end.get(Calendar.MONTH)-start.get(Calendar.MONTH);
monthsBetween += (end.get(Calendar.YEAR)-start.get(Calendar.YEAR))*12;
return monthsBetween;
}
Upvotes: 10