Java Day End daysOfTwoDate(Date beginDate, Date endDate)

Here you can find the source of daysOfTwoDate(Date beginDate, Date endDate)

Description

days Of Two Date

License

Apache License

Declaration

public static int daysOfTwoDate(Date beginDate, 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 int daysOfTwoDate(Date beginDate, Date endDate) {

        Calendar beginCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();

        beginCalendar.setTime(beginDate);
        endCalendar.setTime(endDate);/*from   w w  w. j  a  v a  2 s . c  o m*/

        return getDaysBetween(beginCalendar, endCalendar);

    }

    public static int getDaysBetween(Calendar d1, Calendar d2) {

        int days = 0;
        int years = d1.get(Calendar.YEAR) - d2.get(Calendar.YEAR);
        if (years == 0) {
            days = d2.get(Calendar.DAY_OF_YEAR)
                    - d1.get(Calendar.DAY_OF_YEAR);
            return days;
        } else if (years > 0) {
            for (int i = 0; i < years; i++) {
                d2.add(Calendar.YEAR, 1);
                days += -d2.getActualMaximum(Calendar.DAY_OF_YEAR);
                if (d1.get(Calendar.YEAR) == d2.get(Calendar.YEAR)) {
                    days += d2.get(Calendar.DAY_OF_YEAR)
                            - d1.get(Calendar.DAY_OF_YEAR);
                    return days;
                }
            }
        } else {

            for (int i = 0; i < -years; i++) {
                d1.add(Calendar.YEAR, 1);
                days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
                if (d1.get(Calendar.YEAR) == d2.get(Calendar.YEAR)) {
                    days += d2.get(Calendar.DAY_OF_YEAR)
                            - d1.get(Calendar.DAY_OF_YEAR);
                    return days;
                }
            }

        }

        return days;

    }
}

Related

  1. dateBetweens(Date start, Date end)
  2. dateSpan(Date beginDate, Date endDate)
  3. dayBetweenTwoDates(Date beginDate, Date endDate)
  4. dayEnd(Date dt)
  5. dayEnd(final Date date)
  6. dayValue(Date date, boolean startEnd)
  7. deltaInDays(Date start, Date end)
  8. deltaMonths(Date start, Date end)
  9. divMonth(Date startDate, Date enDate)