Here you can find the source of getYearsDifference(Date startTime, Date endTime)
public static int getYearsDifference(Date startTime, Date endTime)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; import java.util.Date; public class Main { public static int getYearsDifference(Date startTime, Date endTime) { Calendar calendarStart = Calendar.getInstance(); Calendar calendarEnd = Calendar.getInstance(); calendarStart.setTime(startTime); calendarEnd.setTime(endTime);/*from w ww. j a va2 s . c o m*/ return (calendarEnd.get(Calendar.YEAR) - calendarStart.get(Calendar.YEAR)) + (calendarStart.get(Calendar.MONTH) == 0 ? 0 : 1); } public static int getYearsDifference(Date startTime) { return getYearsDifference(startTime, new Date()); } }