Example usage for java.sql Date compareTo

List of usage examples for java.sql Date compareTo

Introduction

In this page you can find the example usage for java.sql Date compareTo.

Prototype

public int compareTo(Date anotherDate) 

Source Link

Document

Compares two Dates for ordering.

Usage

From source file:org.kuali.kfs.vnd.document.service.impl.VendorServiceImpl.java

/**
 * @see org.kuali.kfs.vnd.document.service.VendorService#isVendorContractExpired(org.kuali.rice.krad.document.Document, java.lang.Integer, org.kuali.kfs.vnd.businessobject.VendorDetail)
 *///from   w ww.  j av  a 2  s .c  o m
@Override
public boolean isVendorContractExpired(Document document, Integer vendorContractGeneratedIdentifier,
        VendorDetail vendorDetail) {
    boolean isExpired = false;

    if (ObjectUtils.isNotNull(vendorContractGeneratedIdentifier)) {
        VendorContract vendorContract = SpringContext.getBean(BusinessObjectService.class)
                .findBySinglePrimaryKey(VendorContract.class, vendorContractGeneratedIdentifier);
        Date currentDate = SpringContext.getBean(DateTimeService.class).getCurrentSqlDate();
        List<Note> notes = document.getNotes();

        if ((currentDate.compareTo(vendorContract.getVendorContractEndDate()) > 0
                && (vendorContract.getVendorContractExtensionDate() == null
                        || currentDate.compareTo(vendorContract.getVendorContractExtensionDate()) > 0))
                || !vendorContract.isActive()) {
            Note newNote = new Note();
            newNote.setNoteText("Vendor Contract: " + vendorContract.getVendorContractName()
                    + " contract has expired contract end date.");
            newNote.setNotePostedTimestampToCurrent();
            newNote.setNoteTypeCode(KFSConstants.NoteTypeEnum.BUSINESS_OBJECT_NOTE_TYPE.getCode());
            Note note = noteService.createNote(newNote, vendorDetail,
                    GlobalVariables.getUserSession().getPrincipalId());
            notes.add(note);
            isExpired = true;
        }
    }

    return isExpired;
}

From source file:org.kuali.kra.budget.parameters.BudgetPeriodRule.java

private boolean isValidNewBudgetPeriod(Budget budget, BudgetPeriod newBudgetPeriod) {
    ErrorMap errorMap = GlobalVariables.getErrorMap();
    boolean validNewBudgetPeriod = true;
    List<BudgetPeriod> budgetPeriods = budget.getBudgetPeriods();
    Date previousPeriodStartDate = null;
    Date previousPeriodEndDate = null;
    Date periodStartDate = null;/*from   w w w.  jav  a 2s .  co  m*/
    Date periodEndDate = null;
    Date newPeriodStartDate = null;
    Date newPeriodEndDate = null;
    int index = 0;

    /* check new budget period */
    newPeriodStartDate = newBudgetPeriod.getStartDate();
    newPeriodEndDate = newBudgetPeriod.getEndDate();
    errorMap.addToErrorPath(NEW_BUDGET_PERIOD);
    if (newPeriodStartDate == null) {
        saveErrors("ERROR_PERIOD_START_REQUIRED", errorMap);
        validNewBudgetPeriod = false;
    }
    if (newPeriodEndDate == null) {
        saveErrors("ERROR_PERIOD_END_REQUIRED", errorMap);
        validNewBudgetPeriod = false;
    }
    errorMap.removeFromErrorPath(NEW_BUDGET_PERIOD);

    /* if dates are valid, check further where we can insert this new date */
    if (validNewBudgetPeriod) {
        int totalBudgetPeriods = budgetPeriods.size() - 1;
        errorMap.addToErrorPath(NEW_BUDGET_PERIOD);
        for (BudgetPeriod budgetPeriod : budgetPeriods) {
            Date validDateBefore;
            periodStartDate = budgetPeriod.getStartDate();
            periodEndDate = budgetPeriod.getEndDate();
            String dateCompareValue = null;
            /* check first record */
            if (previousPeriodStartDate == null) {
                validDateBefore = projectStartDate;
            } else {
                validDateBefore = previousPeriodEndDate;
            }
            /* check if entered new period already exists in budget periods list */
            int periodNum = index;
            String[] newPeriodDateParams = { periodNum + "", periodNum + 1 + "" };
            String invalidErrorMessage = null;
            setErrorParameter(newPeriodDateParams);
            if (index == 0 || index == totalBudgetPeriods) {
                invalidErrorMessage = "ERROR_NEW_PERIOD_INVALID";
            } else {
                invalidErrorMessage = "ERROR_NEW_PERIOD_VALID";
            }
            if ((newPeriodStartDate.compareTo(periodStartDate) == 0)
                    || (newPeriodEndDate.compareTo(periodEndDate) == 0)) {
                saveErrors(invalidErrorMessage, errorMap);
                validNewBudgetPeriod = false;
                break;
            } else if (newPeriodStartDate.before(periodStartDate)
                    || (index == totalBudgetPeriods && newPeriodStartDate.after(periodEndDate))) {
                /* check if new period start date is before current period start date */
                boolean lastRecord = false;
                if (index == totalBudgetPeriods) {
                    lastRecord = true;
                    if (newPeriodStartDate.after(periodEndDate)) {
                        periodNum = index + 1;
                    }
                }
                /* check new budget period */
                if (newPeriodStartDate.before(getProjectStartDate())) {
                    dateCompareValue = "ERROR_PERIOD_START_BEFORE_PROJECT_START";
                } else if (newPeriodStartDate.after(getProjectEndDate())) {
                    dateCompareValue = "ERROR_NEW_PERIOD_START_AFTER_PROJECT_END";
                } else if (newPeriodEndDate.after(getProjectEndDate())) {
                    dateCompareValue = "ERROR_NEW_PERIOD_END_DATE";
                } else if (newPeriodStartDate.before(validDateBefore)) {
                    dateCompareValue = invalidErrorMessage;
                } else if ((index < totalBudgetPeriods) && newPeriodEndDate.after(periodStartDate)) {
                    if (!lastRecord) {
                        dateCompareValue = invalidErrorMessage;
                    } else {
                        dateCompareValue = "ERROR_NEW_PERIOD_PROJECT_END";
                    }
                }
                if (dateCompareValue != null) {
                    saveErrors(dateCompareValue, errorMap);
                    validNewBudgetPeriod = false;
                } else {
                    newBudgetPeriod.setBudgetPeriod(periodNum + 1);
                }
                break;
            }
            previousPeriodStartDate = budgetPeriod.getStartDate();
            previousPeriodEndDate = budgetPeriod.getEndDate();
            index++;
        }
        errorMap.removeFromErrorPath(NEW_BUDGET_PERIOD);
    }
    return validNewBudgetPeriod;
}

From source file:org.kuali.kra.budget.rates.BudgetRatesServiceImpl.java

protected Date getRateEffectiveStartDate(Budget budget, AbstractInstituteRate rate, Date personEffectiveDate) {
    Date effectiveDate = budget.getStartDate();
    if (rate.getRateClass().getRateClassType().equalsIgnoreCase(Constants.RATE_CLASS_TYPE_FOR_INFLATION)
            && personEffectiveDate != null && personEffectiveDate.compareTo(effectiveDate) < 0) {
        effectiveDate = personEffectiveDate;
    }/*from w w w .  j  a v  a  2s  . co m*/
    return effectiveDate;
}

From source file:org.kuali.kra.budget.rates.BudgetRatesServiceImpl.java

@SuppressWarnings("unchecked")
protected void getRatesForProjectDates(Budget budget, Collection allRates, Collection filteredRates,
        Date personSalaryEffectiveDate) {
    List<AbstractInstituteRate> dateFilteredRates = (List<AbstractInstituteRate>) filteredRates;
    List<AbstractInstituteRate> allAbstractInstituteRates = (List<AbstractInstituteRate>) allRates;
    for (AbstractInstituteRate rate : allAbstractInstituteRates) {
        Date rateStartDate = rate.getStartDate();
        Date rateEffectiveDate = getRateEffectiveStartDate(budget, rate, personSalaryEffectiveDate);

        if (rateStartDate.compareTo(rateEffectiveDate) >= 0
                && rateStartDate.compareTo(budget.getEndDate()) <= 0) {
            dateFilteredRates.add(rate);
        }/*from   ww  w  .  j  a  va 2 s. c o  m*/
    }
}

From source file:org.kuali.kra.budget.rates.BudgetRatesServiceImpl.java

@SuppressWarnings("unchecked")
protected void getApplicableRates(Budget budget, Collection allRates, Collection filteredRates,
        Date personSalaryEffectiveDate) {
    List<AbstractInstituteRate> allAbstractInstituteRates = (List<AbstractInstituteRate>) allRates;
    Map<String, AbstractInstituteRate> instRates = new HashMap<String, AbstractInstituteRate>();
    for (AbstractInstituteRate instituteRate : allAbstractInstituteRates) {
        Date rateStartDate = instituteRate.getStartDate();
        Date rateEffectiveDate = getRateEffectiveStartDate(budget, instituteRate, personSalaryEffectiveDate);
        if (rateStartDate.before(rateEffectiveDate)) {
            String hKey = generateThreePartKey(instituteRate);
            AbstractInstituteRate instRate = instRates.get(hKey);
            if ((instRate != null) && (instRate.getStartDate().compareTo(rateStartDate) <= 0)) {
                Date currentStartDate = instRate.getStartDate();
                if (currentStartDate.compareTo(rateStartDate) <= 0) {
                    instRates.remove(hKey);
                }//w  w w  .  java 2s. c o m
            }
            if (!instRates.keySet().contains(hKey)) {
                instRates.put(hKey, instituteRate);
            }
        }

    }
    filteredRates.addAll(instRates.values());
}

From source file:org.kuali.kra.budget.summary.BudgetSummaryServiceImpl.java

/**
 * @see org.kuali.kra.budget.summary.BudgetSummaryService#generateBudgetPeriods()
 *//* w  w w.j a  v a 2s .  co  m*/
public void generateBudgetPeriods(Budget budget, List<BudgetPeriod> budgetPeriods) {
    Date projectStartDate = budget.getStartDate();
    Date projectEndDate = budget.getEndDate();
    boolean budgetPeriodExists = true;

    Calendar cl = Calendar.getInstance();

    Date periodStartDate = projectStartDate;
    int budgetPeriodNum = 1;
    while (budgetPeriodExists) {
        cl.setTime(periodStartDate);
        cl.add(Calendar.YEAR, 1);
        Date nextPeriodStartDate = new Date(cl.getTime().getTime());
        cl.add(Calendar.DATE, -1);
        Date periodEndDate = new Date(cl.getTime().getTime());
        /* check period end date gt project end date */
        switch (periodEndDate.compareTo(projectEndDate)) {
        case 1:
            periodEndDate = projectEndDate;
        case 0:
            budgetPeriodExists = false;
            break;
        }
        BudgetPeriod budgetPeriod = budget.getNewBudgetPeriod();
        budgetPeriod.setBudgetPeriod(budgetPeriodNum);
        budgetPeriod.setStartDate(periodStartDate);
        budgetPeriod.setEndDate(periodEndDate);
        budgetPeriod.setBudget(budget);
        budgetPeriod.setBudgetId(budget.getBudgetId());
        budgetPeriods.add(budgetPeriod);
        periodStartDate = nextPeriodStartDate;
        budgetPeriodNum++;
    }
}

From source file:org.kuali.kra.budget.summary.BudgetSummaryServiceImpl.java

/**
 * //w w w  . jav a2 s .  co  m
 * This method is to be shared by adjusting dates for budgetperiod->lineitem and lineitem->personnellineitem
 * refer to jira-1376 for rules
 * @param parentStartDate
 * @param oldStartDate
 * @param parentEndDate
 * @param startEndDates
 * @return
 */
protected List<Date> getNewStartEndDates(Date parentStartDate, Date oldStartDate, Date parentEndDate,
        Date oldEndDate, List<Date> startEndDates) {
    Date startDate = startEndDates.get(0);
    Date endDate = startEndDates.get(1);
    Date newStartDate = startDate;
    Date newEndDate = endDate;
    if (startDate.compareTo(oldStartDate) == 0 && endDate.compareTo(oldEndDate) == 0) {
        // if initiall, both are matching, then keep matching.
        newStartDate = parentStartDate;
        newEndDate = parentEndDate;
    } else {
        // duration has priority over child start date relative to parent start date
        if (parentStartDate.compareTo(oldStartDate) != 0) {
            // keep the gap between child start date and parent start date
            newStartDate = add(newStartDate, KraServiceLocator.getService(DateTimeService.class)
                    .dateDiff(oldStartDate, parentStartDate, false));
            if (newStartDate.after(parentEndDate)) {
                newStartDate = parentStartDate;
            } else {
                if (newStartDate.after(parentStartDate)) {
                    // keep the duration, but the item start date relative to period start date is not maintained.
                    int parentDuration = KraServiceLocator.getService(DateTimeService.class)
                            .dateDiff(parentStartDate, parentEndDate, false);
                    int duration = KraServiceLocator.getService(DateTimeService.class).dateDiff(startDate,
                            endDate, false);
                    int daysTOEndDate = KraServiceLocator.getService(DateTimeService.class)
                            .dateDiff(newStartDate, parentEndDate, false);
                    if (daysTOEndDate < duration) {
                        if (parentDuration > duration) {
                            newEndDate = parentEndDate;
                            newStartDate = add(newEndDate, duration * (-1));
                        } else {
                            // can't keep duration because parent duration is smaller than child initial duration
                            newStartDate = parentStartDate;
                        }
                    }
                }
            }
            newEndDate = add(newStartDate,
                    KraServiceLocator.getService(DateTimeService.class).dateDiff(startDate, endDate, false));
            if (newEndDate.after(parentEndDate)) {
                newEndDate = parentEndDate;
            }
        } else {
            // end date changed
            if (parentEndDate.compareTo(oldStartDate) != 0 && parentEndDate.before(endDate)) {
                if (parentEndDate.after(startDate) && parentEndDate.before(endDate)) {
                    newEndDate = parentEndDate;
                    // try to keep duration
                    newStartDate = add(newEndDate, KraServiceLocator.getService(DateTimeService.class)
                            .dateDiff(endDate, startDate, false));
                    if (newStartDate.before(parentStartDate)) {
                        newStartDate = parentStartDate;
                    }
                } else {
                    if (parentEndDate.before(startDate)) {
                        newStartDate = parentStartDate;
                        newEndDate = add(newStartDate, KraServiceLocator.getService(DateTimeService.class)
                                .dateDiff(startDate, endDate, false));
                        if (newEndDate.after(parentEndDate)) {
                            newEndDate = parentEndDate;
                        }
                    }
                }
            }
        }
    }
    startEndDates.clear();
    startEndDates.add(0, newStartDate);
    startEndDates.add(1, newEndDate);
    return startEndDates;
}

From source file:org.kuali.kra.budget.summary.BudgetSummaryServiceImpl.java

public boolean isLeapDaysInPeriod(Date sDate, Date eDate) {
    Date leapDate;// w w  w  . j  a va2 s  .  c o m
    int sYear = getYear(sDate);
    int eYear = getYear(eDate);
    if (isLeapYear(sDate)) {
        Calendar c1 = Calendar.getInstance();
        c1.clear();
        c1.set(sYear, 1, 29);
        leapDate = new java.sql.Date(c1.getTime().getTime());
        // start date is before 2/29 & enddate >= 2/29
        if (sDate.before(leapDate)) {
            if (eDate.compareTo(leapDate) >= 0) {
                return true;
            }
        } else if (sDate.equals(leapDate)) {
            return true;
        }
    } else if (isLeapYear(eDate)) {
        Calendar c1 = Calendar.getInstance();
        c1.set(eYear, 1, 29);
        leapDate = new java.sql.Date(c1.getTime().getTime());
        if (eDate.compareTo(leapDate) >= 0) {
            return true;
        }
    } else {
        sYear++;
        while (eYear > sYear) {
            if (isLeapYear(sYear)) {
                return true;
            }
            sYear++;
        }
    }
    return false;
}

From source file:org.kuali.kra.proposaldevelopment.hierarchy.service.impl.ProposalHierarchyServiceImpl.java

protected int getCorrespondingParentPeriod(Budget parentBudget, Budget childBudget) {
    int correspondingStart = -1;

    // using start date of first period as start date and end date of last period
    // as end because budget start and end are not particularly reliable
    Date childStart = childBudget.getBudgetPeriod(0).getStartDate();
    Date parentStart = parentBudget.getBudgetPeriod(0).getStartDate();
    Date parentEnd = parentBudget.getBudgetPeriod(parentBudget.getBudgetPeriods().size() - 1).getEndDate();
    // check that child budget starts somewhere during parent budget
    if (childStart.compareTo(parentStart) >= 0 && childStart.compareTo(parentEnd) < 0) {
        // check that child budget starts on one of the budget period starts
        List<BudgetPeriod> parentPeriods = parentBudget.getBudgetPeriods();
        for (int i = 0; i < parentPeriods.size(); i++) {
            if (childStart.equals(parentPeriods.get(i).getStartDate())) {
                correspondingStart = i;/*  w  w w . j  a  v  a2  s. c  o m*/
                break;
            }
        }
    }
    return correspondingStart;
}

From source file:org.kuali.ole.coa.document.validation.impl.AccountRuleTest.java

/**
 *
 * This method tests whether accountRestrictedStatusDate is greater than currentDate or not.
 *///w  ww  . j a v a 2 s .  c  o  m
@Test
public void testHasTemporaryRestrictedStatusCodeButRestrictedStatusDateisGreaterThanCurrentDate() {
    MaintenanceDocument maintDoc = newMaintDoc(newAccount);
    AccountRule rule = (AccountRule) setupMaintDocRule(maintDoc, AccountRule.class);
    boolean result;
    Date date = new Date(getTimestamp(TEST_DATE).getTime());

    newAccount.setAccountRestrictedStatusCode("T");
    newAccount.setAccountRestrictedStatusDate(date);
    result = rule.hasTemporaryRestrictedStatusCodeButRestrictedStatusDateisGreaterThanCurrentDate(newAccount);
    if (date.compareTo(SpringContext.getBean(DateTimeService.class).getCurrentSqlDate()) > 0) {
        assertEquals(true, result);
    } else {
        assertEquals(false, result);
    }
}