Java Day Between getDaysBetween(Date startDate, Date endDate)

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

Description

get Days Between

License

Open Source License

Declaration

public static int getDaysBetween(Date startDate, Date endDate) 

Method Source Code

//package com.java2s;

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

public class Main {

    public static int getDaysBetween(Date startDate, Date endDate) {
        int days = 0;
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startDate);
        startCalendar.set(Calendar.HOUR_OF_DAY, 0);
        startCalendar.set(Calendar.MINUTE, 0);
        startCalendar.set(Calendar.SECOND, 0);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);//  ww  w.  j  a  v  a  2s.c o  m
        endCalendar.set(Calendar.HOUR_OF_DAY, 0);
        endCalendar.set(Calendar.MINUTE, 0);
        endCalendar.set(Calendar.SECOND, 0);

        if (startCalendar.after(endCalendar)) {
            while (startCalendar.after(endCalendar)) {
                days--;
                startCalendar.roll(Calendar.DAY_OF_YEAR, 1);
            }
            return days;
        } else {
            while (startCalendar.before(endCalendar)) {
                days++;
                startCalendar.add(Calendar.DAY_OF_YEAR, 1);
            }
            return days;
        }
    }
}

Related

  1. getDateDiffYear(Date date, int diff, String... format)
  2. getDateWithDiff(Date date, int amount)
  3. getDaysBetween(Date date1, Date date2)
  4. getDaysBetween(Date start, Date end)
  5. getDaysBetween(Date start, Date end)
  6. getDaysBetween(Date startDate, Date endDate)
  7. getDaysBetweenDate(Date begin, Date end)
  8. getDaysDiff(Date startDate, Date endDate)
  9. getDaysDifference(final Date begin, final Date end)