Java Day End yearsBetween(final Date startDate, final Date endDate)

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

Description

years Between

License

Apache License

Declaration

public static long yearsBetween(final Date startDate, final Date endDate) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static long yearsBetween(final Date startDate, final Date endDate) {
        return daysBetween(startDate, endDate) / 365l;
    }/*from  w  w w . j  a  v  a  2  s. c  o  m*/

    public static long daysBetween(final Calendar startDate, final Calendar endDate) {
        long days = 0;
        while (endDate.after(startDate)) {
            endDate.add(Calendar.DATE, -1);
            ++days;
        }

        return days;
    }

    public static long daysBetween(final Date startDate, final Date endDate) {
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();

        start.setTime(startDate);
        end.setTime(endDate);

        return daysBetween(start, end);
    }
}

Related

  1. setTimeToEndOfDay(Date date)
  2. subtractSecond(Date startDate, Date endDate)
  3. toDayEnds(Date date)
  4. toDayInterval(Date startDate, Date endDate)
  5. todayOnOrBetween(Date startDate, Date endDate)