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

intgetManyWeeksDifference(Date a, Date b)
get Many Weeks Difference
int weeks = 0;
Calendar aCal = Calendar.getInstance();
Calendar bCal = Calendar.getInstance();
aCal.setTime(a);
bCal.setTime(b);
Calendar startCal, endCal;
if (aCal.before(bCal)) {
    startCal = aCal;
...
intgetMinuteDiffByTime(Date time1, Date time2)
get Minute Diff By Time
long startMil = 0;
long endMil = 0;
Calendar calendar = Calendar.getInstance();
calendar.setTime(time1);
calendar.set(1900, 1, 1);
startMil = calendar.getTimeInMillis();
calendar.setTime(time2);
calendar.set(1900, 1, 1);
...
intgetMinutesDifference(final Date begin, final Date end)
Calculate diff in minutes between dates.
return (int) getMillisecondDifference(begin, end) / 60000;
intgetMonthDifference(Date from, Date to)
get Month Difference
Calendar fromCalendar = toMidnightCalendar(from);
Calendar toCalendar = toMidnightCalendar(to);
return getMonthDifference(fromCalendar, toCalendar);
intgetMonthsDifference(Date earlierDate, Date laterDate)
Gets the number of months separating the two dates.
Date span = new Date(laterDate.getTime() - earlierDate.getTime());
Date firstDate = new Date(0);
Calendar calendar = Calendar.getInstance();
calendar.setTime(firstDate);
int firstYear = calendar.get(Calendar.YEAR);
int firstMonth = calendar.get(Calendar.MONTH);
calendar.setTime(span);
int spanYear = calendar.get(Calendar.YEAR);
...
intgetNumDaysDiffExclTime(Date date1, Date date2)
This method returns number of days between date1 and date2, including date1 and date2 regardless of the date time.
int result = 0;
Calendar cal = new GregorianCalendar();
cal.setTime(date1);
while (!cal.getTime().after(date2)) {
    cal.add(Calendar.DAY_OF_MONTH, 1);
    result++;
return result;
...
long[]getTimeDifference(Date d1, Date d2)
get Time Difference
long[] result = new long[5];
Calendar cal = Calendar.getInstance();
cal.setTime(d1);
long t1 = cal.getTimeInMillis();
cal.setTime(d2);
long diff = Math.abs(cal.getTimeInMillis() - t1);
final int ONE_DAY = 1000 * 60 * 60 * 24;
final int ONE_HOUR = ONE_DAY / 24;
...
longgetTimeDifference(Date date1, Date date2)
get Time Difference
Calendar dateCalendar1 = Calendar.getInstance();
dateCalendar1.setTime(date1);
Calendar dateCalendar2 = Calendar.getInstance();
dateCalendar2.setTime(date2);
return (dateCalendar1.getTimeInMillis() - dateCalendar2.getTimeInMillis()) / 1000;
longgetTimeDifference(Date otherDate)
Get the Difference in time From a Date to Now.
if (otherDate == null) {
    throw new IllegalArgumentException("Invalid Date Parameter!");
Calendar now = Calendar.getInstance();
now.setTime(new Date());
Calendar aPointInTime = Calendar.getInstance();
aPointInTime.setTime(otherDate);
return aPointInTime.getTimeInMillis() - now.getTimeInMillis();
...
intgetTodayDiff(String diffDate)
get Today Diff
int diffCnt = 0;
int yyyy = 0;
int mm = 0;
int dd = 0;
Date today = new Date();
yyyy = Integer.parseInt(diffDate.substring(0, 4));
mm = Integer.parseInt(diffDate.substring(4, 6));
dd = Integer.parseInt(diffDate.substring(6, 8));
...