Android Calendar Interval Get yearsBetweenDates(Calendar from, Calendar to)

Here you can find the source of yearsBetweenDates(Calendar from, Calendar to)

Description

years Between Dates

License

Open Source License

Declaration

public static int yearsBetweenDates(Calendar from, Calendar to) 

Method Source Code

//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;
    }
}

Related

  1. before(Calendar c, long offset)
  2. daysBetween(Calendar startDate, Calendar endDate)
  3. getExactDiff(final Calendar cal1, final Calendar cal2)