Example usage for org.apache.commons.lang.time DateUtils addDays

List of usage examples for org.apache.commons.lang.time DateUtils addDays

Introduction

In this page you can find the example usage for org.apache.commons.lang.time DateUtils addDays.

Prototype

public static Date addDays(Date date, int amount) 

Source Link

Document

Adds a number of days to a date returning a new object.

Usage

From source file:org.codelabor.common.calendar.services.HolidayCalendarServiceImpl.java

@Override
public Date getBusinessdayDate(Date date, int amount, boolean isBaseDateIncluded)
        throws ParseException, NoSuchDateException, DateOutOfRangeException {
    Date businessdayDate = null;// w  w  w .  ja va2  s.com
    Date tempDate = date;

    if (isBaseDateIncluded) {// ? ?
        if (amount == 0) { // ? ?
            // no such date
        } else if (amount > 0) { // ? ?
            tempDate = DateUtils.addDays(tempDate, -1);
            while (amount > 0) {
                tempDate = DateUtils.addDays(tempDate, 1);
                if (isBusinessday(tempDate)) {
                    businessdayDate = tempDate;
                    amount--;
                }
            }
        } else if (amount < 0) { // ? ?
            tempDate = DateUtils.addDays(tempDate, 1);
            while (amount < 0) {
                tempDate = DateUtils.addDays(tempDate, -1);
                if (isBusinessday(tempDate)) {
                    businessdayDate = tempDate;
                    amount++;
                }
            }
        }

    } else { // ? ?
        if (amount == 0) { // ? ?
            if (this.isBusinessday(tempDate)) {
                businessdayDate = tempDate;
            }
        } else if (amount > 0) { // ? ?
            while (amount > 0) {
                tempDate = DateUtils.addDays(tempDate, 1);
                if (isBusinessday(tempDate)) {
                    businessdayDate = tempDate;
                    amount--;
                }
            }
        } else if (amount < 0) { // ? ?
            while (amount < 0) {
                tempDate = DateUtils.addDays(tempDate, -1);
                if (isBusinessday(tempDate)) {
                    businessdayDate = tempDate;
                    amount++;
                }
            }
        }
    }
    if (businessdayDate == null) {
        throw new NoSuchDateException();
    }
    return businessdayDate;
}

From source file:org.codelabor.common.calendar.services.HolidayCalendarServiceImpl.java

@Override
public Date getHolidayDate(Date date, int amount, boolean isBaseDateIncluded)
        throws ParseException, NoSuchDateException, DateOutOfRangeException {
    Date holidayDate = null;//  www. j av  a  2 s.  c  om
    Date tempDate = date;

    if (isBaseDateIncluded) { // ? ?
        if (amount == 0) { // ? ?
            // no such date
        } else if (amount > 0) { // ? ?
            tempDate = DateUtils.addDays(tempDate, -1);
            while (amount > 0) {
                tempDate = DateUtils.addDays(tempDate, 1);
                if (isHoliday(tempDate)) {
                    holidayDate = tempDate;
                    amount--;
                }
            }
        } else if (amount < 0) { // ? ?
            tempDate = DateUtils.addDays(tempDate, 1);
            while (amount < 0) {
                tempDate = DateUtils.addDays(tempDate, -1);
                if (isHoliday(tempDate)) {
                    holidayDate = tempDate;
                    amount++;
                }
            }
        }
    } else { // ? ?
        if (amount == 0) { // ? ?
            if (this.isHoliday(tempDate)) {
                holidayDate = tempDate;
            }
        } else if (amount > 0) { // ? ?
            while (amount > 0) {
                tempDate = DateUtils.addDays(tempDate, 1);
                if (isHoliday(tempDate)) {
                    holidayDate = tempDate;
                    amount--;
                }
            }
        } else if (amount < 0) { // ? ?
            while (amount < 0) {
                tempDate = DateUtils.addDays(tempDate, -1);
                if (isHoliday(tempDate)) {
                    holidayDate = tempDate;
                    amount++;
                }
            }
        }
    }
    if (holidayDate == null) {
        throw new NoSuchDateException();
    }
    return holidayDate;
}

From source file:org.codelabor.system.calendar.services.HolidayCalendarServiceImpl.java

public Date getBusinessdayDate(Date date, int amount)
        throws ParseException, NoSuchDateException, DateOutOfRangeException {
    Date businessdayDate = null;//  www .ja v a  2s  . co  m
    Date tempDate = date;

    if (amount == 0) {
        if (this.isBusinessday(tempDate)) {
            businessdayDate = tempDate;
        }
    } else if (amount > 0) {
        while (amount > 0) {
            tempDate = DateUtils.addDays(tempDate, 1);
            if (isBusinessday(tempDate)) {
                businessdayDate = tempDate;
                amount--;
            }
        }
    } else if (amount < 0) {
        while (amount < 0) {
            tempDate = DateUtils.addDays(tempDate, -1);
            if (isBusinessday(tempDate)) {
                businessdayDate = tempDate;
                amount++;
            }
        }
    }

    if (businessdayDate == null) {
        throw new NoSuchDateException();
    }
    return businessdayDate;
}

From source file:org.codelabor.system.calendar.services.HolidayCalendarServiceImpl.java

public Date getHolidayDate(Date date, int amount)
        throws ParseException, NoSuchDateException, DateOutOfRangeException {
    Date holidayDate = null;/*from w w w. j a v a  2 s . com*/
    Date tempDate = date;

    if (amount == 0) {
        if (this.isHoliday(tempDate)) {
            holidayDate = tempDate;
        }
    } else if (amount > 0) {
        while (amount > 0) {
            tempDate = DateUtils.addDays(tempDate, 1);
            if (isHoliday(tempDate)) {
                holidayDate = tempDate;
                amount--;
            }
        }
    } else if (amount < 0) {
        while (amount < 0) {
            tempDate = DateUtils.addDays(tempDate, -1);
            if (isHoliday(tempDate)) {
                holidayDate = tempDate;
                amount++;
            }
        }
    }

    if (holidayDate == null) {
        throw new NoSuchDateException();
    }
    return holidayDate;
}

From source file:org.devproof.portal.core.module.user.service.UserServiceImpl.java

@Override
@Transactional/*from w w w . ja v a2  s . c om*/
@Scheduled(cron = "0 0 4 * * ?")
public void deleteUnconfirmedUser() {
    Date confirmationBorder = DateUtils.addDays(new Date(), -14);
    userRepository.deleteUnconfirmedUserOlderThan(confirmationBorder);
}

From source file:org.eclipse.jubula.client.core.persistence.TestResultPM.java

/**
 * clean test result details by age (days of existence)
 * testrun summaries will not be deleted
 * @param days days/* w  w w  .  j  av a2s .c o m*/
 * @param projGUID the project guid
 * @param majorVersion the project major version number
 * @param minorVersion the project minor version number
 */
public static final void cleanTestresultDetails(int days, String projGUID, int majorVersion, int minorVersion) {
    Date cleanDate = DateUtils.addDays(new Date(), days * -1);
    try {
        Set<Long> summaries = TestResultSummaryPM.findTestResultSummariesByDate(cleanDate, projGUID,
                majorVersion, minorVersion);
        for (Long summaryId : summaries) {
            deleteTestresultOfSummary(summaryId);
        }
        DataEventDispatcher.getInstance().fireTestresultChanged(TestresultState.Refresh);
    } catch (JBException e) {
        throw new JBFatalException(Messages.DeletingTestresultsFailed, e, MessageIDs.E_DELETE_TESTRESULT);
    }
}

From source file:org.eclipse.jubula.client.ui.views.TestresultSummaryView.java

/**
 * refresh view//from  w  w  w.ja  va2s . c  om
 */
public void loadViewInput() {
    m_tableViewer.getControl().getDisplay().asyncExec(new Runnable() {
        public void run() {
            List<ITestResultSummaryPO> metaList;
            try {
                int maxNoOfDays = Plugin.getDefault().getPreferenceStore()
                        .getInt(Constants.MAX_NUMBER_OF_DAYS_KEY);
                Date startTime = DateUtils.addDays(new Date(), maxNoOfDays * -1);
                metaList = TestResultSummaryPM.findAllTestResultSummaries(startTime);
                if (Persistor.instance() != null) {
                    m_detailedSummaryIds = TestResultPM
                            .computeTestresultIdsWithDetails(GeneralStorage.getInstance().getMasterSession());
                }

                if (metaList != null) {
                    m_tableViewer.setInput(metaList.toArray());
                }
            } catch (JBException e) {
                String msg = Messages.CantLoadMetadataFromDatabase;
                log.error(msg, e);
                showErrorDialog(msg);
            }
            // re-set the selection as this could otherwise lead to cached selected
            // POs which are not up-to-date and lead to db-problems on
            // EntityManager.merge();
            ISelection s = m_tableViewer.getSelection();
            m_tableViewer.setSelection(null);
            m_tableViewer.setSelection(s);
        }
    });
}

From source file:org.gbif.portal.web.controller.dataset.DataProviderLogController.java

/**
 * Adds the drop down content./* ww  w  .  ja va  2 s.  c  o  m*/
 * @param request
 * @param mav
 */
@SuppressWarnings("unchecked")
private void addDropDownContent(HttpServletRequest request, ModelAndView mav) {

    //add today and a year from today
    Date today = new Date(System.currentTimeMillis());
    mav.addObject("today", today);
    Date lastWeek = DateUtils.addDays(today, -6);
    mav.addObject("lastWeek", lastWeek);
    Date oneMonthAgo = DateUtils.addMonths(today, -1);
    mav.addObject("oneMonthAgo", oneMonthAgo);
    Date oneYearAgo = DateUtils.addYears(today, -1);
    mav.addObject("oneYearAgo", oneYearAgo);

    //add event enumerations
    Collection<LogEvent> logEvents = (Collection) LogEvent.getValueMap().values();
    //split into 4 categories - Harvest, Extract, User and Other
    List<KeyValueDTO> harvestEvents = new ArrayList<KeyValueDTO>();
    List<KeyValueDTO> extractEvents = new ArrayList<KeyValueDTO>();
    List<KeyValueDTO> userEvents = new ArrayList<KeyValueDTO>();
    List<KeyValueDTO> usageEvents = new ArrayList<KeyValueDTO>();
    List<KeyValueDTO> otherEvents = new ArrayList<KeyValueDTO>();

    Locale locale = RequestContextUtils.getLocale(request);

    for (LogEvent logEvent : logEvents) {
        String name = messageSource.getMessage(logEvent.getName(), null, logEvent.getName(), locale);
        KeyValueDTO keyValue = new KeyValueDTO(logEvent.getValue().toString(), name);
        if (logEvent.getValue() >= LogEvent.HARVEST_RANGE_START
                && logEvent.getValue() <= LogEvent.HARVEST_RANGE_END)
            harvestEvents.add(keyValue);
        else if (logEvent.getValue() >= LogEvent.EXTRACT_RANGE_START
                && logEvent.getValue() <= LogEvent.EXTRACT_RANGE_END)
            extractEvents.add(keyValue);
        else if (logEvent.getValue() >= LogEvent.USER_RANGE_START
                && logEvent.getValue() <= LogEvent.USER_RANGE_END)
            userEvents.add(keyValue);
        else if (logEvent.getValue() >= LogEvent.USAGE_RANGE_START
                && logEvent.getValue() <= LogEvent.USAGE_RANGE_END)
            usageEvents.add(keyValue);
        else
            otherEvents.add(keyValue);
    }

    //sort alphabetically
    Collections.sort(harvestEvents, new KeyValueDTO.ValueComparator());
    Collections.sort(extractEvents, new KeyValueDTO.ValueComparator());
    Collections.sort(userEvents, new KeyValueDTO.ValueComparator());
    Collections.sort(otherEvents, new KeyValueDTO.ValueComparator());

    if (!harvestEvents.isEmpty() && harvestEvents.size() > 1) {
        harvestEvents.add(0, new KeyValueDTO(allHarvestEventsRequestKey,
                messageSource.getMessage("harvestAll", null, "Harvest - all", locale)));
    }
    mav.addObject("harvestEvents", harvestEvents);

    if (!extractEvents.isEmpty() && extractEvents.size() > 1) {
        extractEvents.add(0, new KeyValueDTO(allExtractEventsRequestKey,
                messageSource.getMessage("extractAll", null, "Extract - all", locale)));
        extractEvents.add(1, new KeyValueDTO(allExtractIssuesEventsRequestKey,
                messageSource.getMessage("extractAllIssues", null, "Extract - all issues", locale)));
    }
    mav.addObject("extractEvents", extractEvents);

    if (!userEvents.isEmpty() && userEvents.size() > 1) {
        userEvents.add(0, new KeyValueDTO(allUserEventsRequestKey,
                messageSource.getMessage("userAll", null, "User - all", locale)));
    }
    mav.addObject("userEvents", userEvents);

    if (!usageEvents.isEmpty() && usageEvents.size() > 1) {
        usageEvents.add(0, new KeyValueDTO(allUsageEventsRequestKey,
                messageSource.getMessage("usageAll", null, "Usage - all", locale)));
    }
    mav.addObject("usageEvents", usageEvents);
    mav.addObject("otherEvents", otherEvents);
}

From source file:org.gbif.portal.web.controller.occurrence.OccurrenceFilterWizardController.java

/**
 * @see org.springframework.web.servlet.mvc.Controller#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 *///from   w ww  .ja va  2  s  .co m
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String filterId = request.getParameter("filterId");
    String currValue = request.getParameter("currValue");
    FilterDTO filterDTO = FilterUtils.getFilterById(filterMapWrapper.getFilters(), filterId);
    String viewName = filterDTO.getWizardView();
    ModelAndView mav = new ModelAndView(viewName);
    mav.addObject("messageSource", messageSource);

    if (logger.isDebugEnabled()) {
        logger.debug("filter id:" + filterId);
        logger.debug("current value:" + currValue);
        logger.debug("wizard view name:" + viewName);
    }

    //check for bounding box filter
    if (filterId.equals(boundingBoxFilter.getId()) && StringUtils.isNotEmpty(currValue)) {
        LatLongBoundingBox llbb = BoundingBoxFilterHelper.getLatLongBoundingBox(currValue);
        if (llbb != null)
            mav.addObject("boundingBox", llbb);
    }

    //check for classification filter
    if (filterId.equals(classificationFilter.getId())) {
        DataProviderDTO dataProvider = dataResourceManager.getNubDataProvider();
        List<BriefTaxonConceptDTO> concepts = null;
        if (StringUtils.isNotEmpty(currValue)) {
            concepts = taxonomyManager.getClassificationFor(currValue, false, null, true);
            List<BriefTaxonConceptDTO> childConcepts = taxonomyManager.getChildConceptsFor(currValue, true);
            BriefTaxonConceptDTO selectedConcept = taxonomyManager.getBriefTaxonConceptFor(currValue);
            taxonConceptUtils.organiseUnconfirmedNames(request, selectedConcept, concepts, childConcepts);
            mav.addObject("selectedConcept", selectedConcept);
        } else {
            // we always use the nub resource 
            concepts = taxonomyManager.getRootTaxonConceptsForTaxonomy(null, "1");
        }
        if (dataProvider != null)
            mav.addObject("dataProvider", dataProvider);
        if (concepts != null)
            mav.addObject("concepts", concepts);
    }

    //check for dataResource filter
    if (filterId.equals(dataResourceIdFilter.getId())) {
        List<KeyValueDTO> dataProviderList = dataResourceManager.getDataProviderList();
        mav.addObject("dataProviders", dataProviderList);
        List<KeyValueDTO> resourceNetworkList = dataResourceManager.getResourceNetworkList();
        mav.addObject("networks", resourceNetworkList);
    }

    //check for geoRegion filter
    if (filterId.equals(geoRegionFilter.getId())) {
        List<KeyValueDTO> countryList = geospatialManager.getCountryList();
        mav.addObject("countries", countryList);
    }

    //check for occurrence date filter
    if (filterId.equals(occurrenceDateFilter.getId())) {
        Date today = new Date(System.currentTimeMillis());
        mav.addObject("today", today);
        Date lastWeek = DateUtils.addDays(today, -6);
        mav.addObject("lastWeek", lastWeek);
        Date oneMonthAgo = DateUtils.addMonths(today, -1);
        mav.addObject("oneMonthAgo", oneMonthAgo);
        Date sixMonthsAgo = DateUtils.addMonths(today, -5);
        mav.addObject("sixMonthsAgo", sixMonthsAgo);
        Date oneYearAgo = DateUtils.addYears(today, -1);
        mav.addObject("oneYearAgo", oneYearAgo);
        Date fiveYearsAgo = DateUtils.addYears(today, -5);
        mav.addObject("fiveYearsAgo", fiveYearsAgo);
        Date tenYearsAgo = DateUtils.addYears(today, -10);
        mav.addObject("tenYearsAgo", tenYearsAgo);
    }

    //check for year range filter
    if (filterId.equals(yearRangeFilter.getId())) {
        Date today = new Date(System.currentTimeMillis());
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTime(today);
        int thisYear = calendar.get(Calendar.YEAR);
        mav.addObject("thisYear", thisYear);
    }

    mav.addObject("filterId", filterId);
    return mav;
}

From source file:org.hibernate.search.test.performance.task.InsertBookTask.java

@Override
protected void execute(FullTextSession fts) {
    long bookId = getNextBookId();
    long authorId = getNextAuthorId();

    if (VERBOSE) {
        if (bookId % 100 == 0) {
            log("InsertBookTask: bookId=" + bookId);
        }//  w  w w .  j  a va 2s.  c  om
    }

    Book book = new Book();
    book.setId(bookId);
    book.setTitle("title" + bookId);
    book.setSummary(SUMMARIES[(int) (bookId % SUMMARIES.length)]);
    book.setRating(0.0f);
    book.setTotalSold(0L);
    book.setPublicationDate(DateUtils.addDays(PUBLICATION_DATE_ZERO, (int) (bookId % 1000)));
    book.getAuthors().add(new Author(authorId, "author" + authorId));

    fts.merge(book);
}