Here you can find the source of yearsBetweenDates(Calendar from, Calendar to)
public static int yearsBetweenDates(Calendar from, Calendar to)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; public class Main { public static int yearsBetweenDates(Calendar from, Calendar to) { if (from.after(to)) { Calendar temp = to;//from w ww . jav a2 s . c o m to = from; from = temp; } int years = to.get(Calendar.YEAR) - from.get(Calendar.YEAR); if (years != 0) { // increment months by 1 because are zero based int fromSpan = (from.get(Calendar.MONTH) + 1) * 100 + from.get(Calendar.DAY_OF_MONTH); int toSpan = (to.get(Calendar.MONTH) + 1) * 100 + to.get(Calendar.DAY_OF_MONTH); if (toSpan < fromSpan) { --years; } } return years; } }