Java Day End getYearsBetween(Date startDate, Date endDate)

Here you can find the source of getYearsBetween(Date startDate, Date endDate)

Description

get Years Between

License

Open Source License

Declaration

public static long getYearsBetween(Date startDate, Date endDate) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static final long MILIS_PER_YEAR = 31540000000L;

    public static long getYearsBetween(Date startDate, Date endDate) {
        long msBetween = getMsBetween(startDate, endDate);
        return miliSecondsToYears(msBetween);
    }/*  w  w w . j a va2 s .  c o m*/

    public static long getMsBetween(Date startDate, Date endDate) {
        Calendar cal = getCalendar();
        cal.setTime(startDate);
        long startMs = cal.getTimeInMillis();
        cal.setTime(endDate);
        long endMs = cal.getTimeInMillis();

        return endMs - startMs;
    }

    public static long miliSecondsToYears(long ms) {
        return ms / MILIS_PER_YEAR;
    }

    public static Calendar getCalendar() {
        return getCalendar("GMT");
    }

    public static Calendar getCalendar(String timezone) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone(timezone));

        return cal;
    }
}

Related

  1. getTotalDays(Date startDate, Date endDate)
  2. getTotalMonths(final Date startDateToCheck, final Date endDateToCheck)
  3. getTrendTime(Date date)
  4. getWorkingDays(Calendar start, Calendar end, final Long[] holidays)
  5. getWorkingDaysBetweenTwoDates(Date startDate, Date endDate)
  6. getYearsBetweenDate(Date begin, Date end)
  7. isBetweenDate(Date date, Date startDate, Date endDate)
  8. isBetWeenDates(Date in, Date from, Date to)
  9. isCalenderDaySame(GregorianCalendar aInCal1, GregorianCalendar aInCal2)