Java Utililty Methods Day Between

List of utility methods to do Day Between

Description

The list of methods to do Day Between are organized into topic(s).

Method

intgetWorkingDaysBetween(Date fromDate, Date toDate)
get Working Days Between
return getWorkingDaysBetween(getCalendar(fromDate), getCalendar(toDate));
intgetYearDiff(Date date1, Date date2)
get Year Diff
if (date1 == null || date2 == null) {
    return 0;
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
int year1 = calendar1.get(Calendar.YEAR);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
...
intgetYearsDifference(Date startTime, Date endTime)
get Years Difference
Calendar calendarStart = Calendar.getInstance();
Calendar calendarEnd = Calendar.getInstance();
calendarStart.setTime(startTime);
calendarEnd.setTime(endTime);
return (calendarEnd.get(Calendar.YEAR) - calendarStart.get(Calendar.YEAR))
        + (calendarStart.get(Calendar.MONTH) == 0 ? 0 : 1);
StringhourDiff(Date firstDate, Date lastDate)
hour Diff
Calendar cal1 = new GregorianCalendar(TimeZone.getDefault());
Calendar cal2 = new GregorianCalendar(TimeZone.getDefault());
cal1.setTime(firstDate);
cal2.setTime(lastDate);
int d1 = cal1.get(Calendar.DAY_OF_YEAR);
int d2 = cal2.get(Calendar.DAY_OF_YEAR);
int h1 = cal1.get(Calendar.HOUR_OF_DAY);
int h2 = cal2.get(Calendar.HOUR_OF_DAY);
...
DateinternalTrimOrAlterDate(Date date, boolean trim, int dayDiff)
internal Trim Or Alter Date
if (null == date) {
    return null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if (trim) {
    cal.set(Calendar.MILLISECOND, 0);
    cal.set(Calendar.SECOND, 0);
...
intmonthDiff(Date beginDate, Date endDate)
month Diff
if (beginDate == null || endDate == null) {
    return 0;
Calendar beginCal = Calendar.getInstance();
beginCal.setTime(beginDate);
Calendar endCal = Calendar.getInstance();
endCal.setTime(endDate);
int yearDiff = endCal.get(Calendar.YEAR) - beginCal.get(Calendar.YEAR);
...
intmonthDiff(Date from, Date to)
month Diff
Calendar fromCal = Calendar.getInstance();
Calendar toCal = Calendar.getInstance();
fromCal.setTime(from);
toCal.setTime(to);
int monthDiff = (toCal.get(Calendar.YEAR) - fromCal.get(Calendar.YEAR)) * 12
        + (toCal.get(Calendar.MONTH) - fromCal.get(Calendar.MONTH));
return monthDiff;
intmonthDiff(Date fromDate, Date toDate)
month Diff
long dateDiff = dateDiff(fromDate, toDate);
int monthDiff = (int) (dateDiff / DAY_PER_MONTH);
if (addMonths(fromDate, monthDiff).after(toDate)) {
    monthDiff--;
return monthDiff;
intmonthDifference(final Date a, final Date b)
month Difference
final Calendar calendarA = Calendar.getInstance();
final Calendar calendarB = Calendar.getInstance();
int multiplier;
if (b.getTime() - a.getTime() > 0) {
    multiplier = -1;
    calendarA.setTime(b);
    calendarB.setTime(a);
} else {
...
intmonthsDiff(final Date data1, final Date data2)
Get the difference in months of two given dates.
Calendar cal = Calendar.getInstance();
cal.setTime(data1);
int monthA = cal.get(Calendar.MONTH);
int yearA = cal.get(Calendar.YEAR);
cal.setTime(data2);
int monthB = cal.get(Calendar.MONTH);
int yearB = cal.get(Calendar.YEAR);
return ((yearA - yearB) * cal.getMaximum(Calendar.MONTH)) + (monthA - monthB);
...