Java Year differenceInYears(final Calendar a, final Calendar b)

Here you can find the source of differenceInYears(final Calendar a, final Calendar b)

Description

Calculates difference in years between to dates.

License

Open Source License

Parameter

Parameter Description
a first element of substraction of dates.
b second element of substraction of dates.

Return

a-b in fractions of year.

Declaration

public static double differenceInYears(final Calendar a, final Calendar b) 

Method Source Code

//package com.java2s;
/*/*from  ww w.  j  av  a2 s. co  m*/
 * Copyright 2011 - 2012
 * All rights reserved. License and terms according to LICENSE.txt file.
 * The LICENSE.txt file and this header must be included or referenced 
 * in each piece of code derived from this project.
 */

import java.util.*;

public class Main {
    private static final long MILLISECONDS_PER_YEAR = 365 * 24 * 60 * 60 * 1000;

    /**
     * Calculates difference in years between to dates.
     * @param a first element of substraction of dates.
     * @param b second element of substraction of dates.
     * @return a-b in fractions of year.
     */
    public static double differenceInYears(final Calendar a, final Calendar b) {
        final long millisDif = a.getTimeInMillis() - b.getTimeInMillis();
        return millisDif / MILLISECONDS_PER_YEAR;
    }
}

Related

  1. createCalendarAtBeginningOfYear(int year)
  2. currentYear()
  3. daysInYear(int year)
  4. diffYears(Date day1, Date day2)
  5. IsInLeapYear(Date date1)
  6. isLeapYear(int year)
  7. nextYears(int diff)