public static int findYearsInBetween(Date startDate, Date endDate){
Calendar startCal = getCalendar(startDate);
Calendar endCal = getCalendar(endDate);
int diff = endCal.get(Calendar.YEAR) - startCal.get(Calendar.YEAR);
if (startCal.get(Calendar.MONTH) > endCal.get(Calendar.MONTH) ||
(startCal.get(Calendar.MONTH) == endCal.get(Calendar.MONTH)
&& startCal.get(Calendar.DATE) > endCal.get(Calendar.DATE))) {
diff--;
}
return diff;
}
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
String startDateString = <Give start date here>;
String endDateString = <Give end date here>;
Date startDate = formatter.parse(startDateString);
Date endDate = formatter.parse(endDateString);
System.out.print("No of days :"+ findYearsInBetween(startDate,endDate));
It's great to find you back here finally.
ReplyDeleteIsn't this a better solution:
LocalDate start = LocalDate.of( 2010 , 1 , 1 ) ;
LocalDate stop = LocalDate.now( ZoneId.of( "America/Montreal" ) );
long years = java.time.temporal.ChronoUnit.YEARS.between( start , stop );
Ratnesh But its available with JAVA 8 only,It is very easy to handle date, time, calender and locale with it and it will be integrated to java in version 8
Delete