Java TimeUnit Usage daysBetween(Date initDate, Date endDate)

Here you can find the source of daysBetween(Date initDate, Date endDate)

Description

days Between

License

Open Source License

Declaration

public static long daysBetween(Date initDate, 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.concurrent.TimeUnit;

public class Main {
    public static long daysBetween(Date initDate, Date endDate) {
        return daysBetween(initDate, endDate, true);
    }//  w  w w.j  a  v a  2 s .c om

    public static long daysBetween(Date initDate, Date endDate, Boolean includeEndDate) {
        return getDifference(toDateCalendar(initDate), toDateCalendar(endDate), TimeUnit.DAYS)
                + (includeEndDate ? 1 : 0);
    }

    public static long getDifference(Calendar initDate, Calendar endDate, TimeUnit units) {
        return units.convert(endDate.getTimeInMillis() - initDate.getTimeInMillis(), TimeUnit.MILLISECONDS);
    }

    public static Calendar toDateCalendar(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(date.getTime());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar;
        }
        return null;
    }
}

Related

  1. createTimezoneString(String timezoneID)
  2. currentMillisFromNanotime()
  3. dateDiff(Date d1, Date d2)
  4. dateStart(final Calendar c)
  5. daysBetween(Calendar startDate, Calendar endDate)
  6. daysBetweenDates(long toDate, long fromDate)
  7. daysFromToday(long toDate)
  8. daysToMillis(int days)
  9. durationInSecs(long startNanos, long endNanos)