Reputation: 1204
Sorry, I tried referred the methods from Similar Method. But i still cannot get solution. How can i get the output months and days between two difference dates? Any helps would be appreciated.Thanks.
P/s : JodaTime not applicable.
What i've tried so far.
SimpleDateFormat formatter= new SimpleDateFormat("dd-MM-yyyy");
String start = "03-01-2015";
String end = "01-11-2014";
Date startdate = formatter.parse(start);
Date enddate = formatter.parse(end);
Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startdate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(enddate);
int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) -startCalendar.get(Calendar.MONTH);
Upvotes: 0
Views: 126
Reputation: 2873
Hope this is what you want.
int diffDay= endCalendar.get(Calendar.DAY_OF_MONTH) -startCalendar.get(Calendar.DAY_OF_MONTH);
System.err.println("diffMonth==="+diffMonth +" Month(s) and " + diffDay + " Day(s)");
Upvotes: 1
Reputation: 1708
In java8 you can do it like this:
LocalDate startDate = LocalDate.of(2004, Month.JANUARY, 1);
LocalDate endDate = LocalDate.of(2005, Month.JANUARY, 1);
long monthsInYear2 = ChronoUnit.MONTHS.between(startDate, endDate);
(source: http://www.leveluplunch.com/java/examples/number-of-months-between-two-dates/)
Upvotes: 0