Java Utililty Methods Date Calculate

List of utility methods to do Date Calculate

Description

The list of methods to do Date Calculate are organized into topic(s).

Method

DatecalculateDate(Date date, int field, int value)
calculate Date
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(field, value);
return c.getTime();
intcalculateDays(Date startDate, Date endDate)
calculate Days
int presumedDays = 0;
Calendar startCal = Calendar.getInstance();
startCal.setTime(startDate);
Calendar endCal = Calendar.getInstance();
endCal.setTime(endDate);
if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) {
    return 0;
while (startCal.before(endDate)) {
    startCal.add(Calendar.DAY_OF_MONTH, 1);
    presumedDays++;
return presumedDays;
DatecalculateDurationEndInSec(Date start, int durationInSeconds)
calculate Duration End In Sec
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.add(Calendar.SECOND, durationInSeconds);
return new Date(cal.getTimeInMillis());
DatecalculateEndDate(Date startDate, int duration)
calculate End Date
Calendar startCal = Calendar.getInstance();
startCal.setTime(startDate);
for (int i = 1; i < duration; i++) {
    startCal.add(Calendar.DAY_OF_MONTH, 1);
    while (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
            || startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        startCal.add(Calendar.DAY_OF_MONTH, 1);
return startCal.getTime();
intcalculateGestationAge(Date today, Date edd)
calculate Gestation Age
int i = 40;
Calendar calendar = Calendar.getInstance();
calendar.setTime(edd);
if (today.before(edd)) {
    while (today.before(calendar.getTime()) || today.equals(calendar.getTime())) {
        i--;
        calendar.add(Calendar.DAY_OF_YEAR, -7);
        if (i < 0) {
...
DatecalculateLookbackDate(int lookback)
calculate Lookback Date
Date dayCounter = new Date();
int validDatesCounter = lookback;
while (validDatesCounter > 0) {
    dayCounter.setDate(dayCounter.getDate() - 1);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dayCounter);
    if (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
            && calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY && !holidayList.contains(dayCounter)) {
...
intcalculateMonthIn(Date date1, Date date2)
calculate Month In
Calendar cal1 = new GregorianCalendar();
cal1.setTime(date1);
Calendar cal2 = new GregorianCalendar();
cal2.setTime(date2);
int c = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12 + cal1.get(Calendar.MONTH)
        - cal2.get(Calendar.MONTH);
return Math.abs(c);
DatecalculateNextSettleDate(Date date, int addMonth)
calculate Next Settle Date
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int maxOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
if (dayOfMonth == maxOfMonth) {
    calendar.add(Calendar.MONTH, addMonth);
    maxOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    calendar.set(Calendar.DAY_OF_MONTH, maxOfMonth);
...
LongcalculateOffsetFromGMT(Date date)
Calculates a date time offset from GMT.
Calendar cal = Calendar.getInstance();
long offset = cal.getTimeZone().getOffset(date.getTime());
return offset;
DatecalculatePastDate(int days)
Returns date in history: Today - the given number of days
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -days);
return cal.getTime();