Example usage for org.joda.time DateTime minusMonths

List of usage examples for org.joda.time DateTime minusMonths

Introduction

In this page you can find the example usage for org.joda.time DateTime minusMonths.

Prototype

public DateTime minusMonths(int months) 

Source Link

Document

Returns a copy of this datetime minus the specified number of months.

Usage

From source file:rapture.dp.invocable.ArchiveRepoStep.java

License:Open Source License

@Override
public String invoke(CallingContext context) {

    List<String> docRepoNames = readListFromContext(context, DOC_REPO_NAMES);
    List<String> sysRepoNames = readListFromContext(context, SYS_REPO_NAMES);

    String versionLimitStr = Kernel.getDecision().getContextValue(context, getWorkerURI(), VERSION_LIMIT);
    String timeLimitStr = Kernel.getDecision().getContextValue(context, getWorkerURI(), TIME_LIMIT);
    if (StringUtils.isEmpty(versionLimitStr) && StringUtils.isEmpty(timeLimitStr)) {
        log.warn("No version limit or time limit is specified, nothing to archive");
        return NEXT;
    }/*from  w  w  w. j av  a2 s .c  om*/

    int versionLimit = StringUtils.isEmpty(versionLimitStr) ? Integer.MAX_VALUE
            : Integer.parseInt(versionLimitStr);
    long timeLimit = 0;
    if (!StringUtils.isEmpty(timeLimitStr)) {
        DateTime dateTime = DateTime.now();
        int number = Integer.valueOf(timeLimitStr.substring(0, timeLimitStr.length() - 1));
        char dateUnit = timeLimitStr.charAt(timeLimitStr.length() - 1);
        switch (dateUnit) {
        case 'M':
            dateTime = dateTime.minusMonths(number);
            break;
        case 'W':
            dateTime = dateTime.minusWeeks(number);
            break;
        case 'D':
            dateTime = dateTime.minusDays(number);
            break;
        default:
            throw RaptureExceptionFactory.create("Invalid date unit " + dateUnit);
        }
        timeLimit = dateTime.getMillis();
    }

    boolean ensureVersionLimit = Boolean
            .parseBoolean(Kernel.getDecision().getContextValue(context, getWorkerURI(), ENSURE_VERSION_LIMIT));

    log.info(String.format("Configured to archive %s doc repos and %s system repos.", docRepoNames.size(),
            sysRepoNames.size()));

    for (String repoName : docRepoNames) {
        log.info(String.format("About to archive doc repo %s", repoName));
        Kernel.getDoc().archiveRepoDocs(context, repoName, versionLimit, timeLimit, ensureVersionLimit);
    }

    for (String repoName : sysRepoNames) {
        Repository repo = Kernel.INSTANCE.getRepo(repoName);
        if (repo == null) {
            log.info(String.format("Repo not found for name '%s'", repoName));
        } else {
            if (repo.isVersioned() && repo instanceof NVersionedRepo) {
                log.info(String.format("About to archive sys repo %s", repoName));
                ((NVersionedRepo) repo).archiveRepoVersions(repoName, versionLimit, timeLimit,
                        ensureVersionLimit, context.getUser());
            } else {
                log.info(String.format(
                        "Sys repo %s is not of type NVersionedRepo, instead it is of type %s, skipping...",
                        repoName, repo.getClass().getName()));
            }
        }
    }

    return NEXT;
}

From source file:rapture.dp.invocable.CheckPrerequisiteStep.java

License:Open Source License

private DateTime getDateTime(DateTime dateTime, String dateWithin, String timeNoEarlierThan) {
    if (!StringUtils.isEmpty(timeNoEarlierThan)) {
        DateTime hours = getDateTime(timeNoEarlierThan);
        if (dateTime == null)
            dateTime = hours;//w ww.j av a2 s.  com
        else {
            dateTime = dateTime.withMillisOfDay(hours.getMillisOfDay()).withZone(hours.getZone());
        }
    }
    if (!StringUtils.isEmpty(dateWithin)) {
        int number = Integer.valueOf(dateWithin.substring(0, dateWithin.length() - 1));
        char dateUnit = dateWithin.charAt(dateWithin.length() - 1);
        switch (dateUnit) {
        case 'M':
            dateTime = dateTime.minusMonths(number);
            break;
        case 'W':
            dateTime = dateTime.minusWeeks(number);
            break;
        case 'D':
            dateTime = dateTime.minusDays(number);
            break;
        case 'H':
            dateTime = dateTime.minusHours(number);
            break;
        case 'B':
            dateTime = USCalendar.minusBusinessDays(dateTime, number);
            break;
        default:
            throw RaptureExceptionFactory.create("Invalid date unit " + dateUnit);
        }
    }
    return dateTime;
}

From source file:uk.org.rbc1b.roms.db.volunteer.HibernateVolunteerDao.java

License:Open Source License

@Override
public List<Volunteer> findVolunteersWhoNeedBiannualEmail(VolunteerSearchCriteria searchCriteria) {
    Session session = this.sessionFactory.getCurrentSession();
    Criteria criteria = createVolunteerSearchCriteria(searchCriteria, session);

    DateTime todayLocalDate = new DateTime();
    DateTime sixMonthsBehindLocalDate = todayLocalDate.minusMonths(6);

    Date todayDate = DataConverterUtil.toSqlDate(todayLocalDate);
    Date sixMonthsBehind = DataConverterUtil.toSqlDate(sixMonthsBehindLocalDate);

    criteria.add(Restrictions.and(/*from w w w . j a  v  a2s . co m*/
            Restrictions.or(Restrictions.isNull("updateContactDetailsEmailLastSent"),
                    Restrictions.not(Restrictions.between("updateContactDetailsEmailLastSent", sixMonthsBehind,
                            todayDate))),
            Restrictions.or(Restrictions.isNull("contactDetailsLastConfirmed"), Restrictions
                    .not(Restrictions.between("contactDetailsLastConfirmed", sixMonthsBehind, todayDate)))));

    if (searchCriteria.getMaxResults() != null) {
        criteria.setMaxResults(searchCriteria.getMaxResults());
    }

    criteria.addOrder(Order.asc("person.personId"));

    return criteria.list();
}

From source file:uk.org.rbc1b.roms.db.volunteer.HibernateVolunteerDao.java

License:Open Source License

@Override
public List<Volunteer> findVolunteersWhoNeedToSubmitLDCForm(VolunteerSearchCriteria searchCriteria) {
    Session session = this.sessionFactory.getCurrentSession();
    Criteria criteria = createVolunteerSearchCriteria(searchCriteria, session);

    criteria.add(Restrictions.eq("submitNewLDCFormEmailSent", false));

    DateTime twoAndHalfYearsInPast = new DateTime();
    twoAndHalfYearsInPast = twoAndHalfYearsInPast.minusYears(2);
    twoAndHalfYearsInPast = twoAndHalfYearsInPast.minusMonths(6);

    Date twoAndHalfYearsDate = DataConverterUtil.toSqlDate(twoAndHalfYearsInPast);

    criteria.add(//www . ja  v a 2  s .  c o m
            Restrictions.or(Restrictions.isNull("formDate"), Restrictions.le("formDate", twoAndHalfYearsDate)));

    if (searchCriteria.getMaxResults() != null) {
        criteria.setMaxResults(searchCriteria.getMaxResults());
    }

    criteria.addOrder(Order.asc("person.personId"));

    return criteria.list();
}

From source file:utils.DateUtils.java

License:Open Source License

public static Date findFirstDayOfPastMonth() {
    DateTime dt = new DateTime();

    return dt.minusMonths(1).dayOfMonth().withMinimumValue().toDate();
}

From source file:utils.DateUtils.java

License:Open Source License

public static Date findLastDayOfPastMonth() {
    DateTime dt = new DateTime();

    return dt.minusMonths(1).dayOfMonth().withMaximumValue().toDate();
}

From source file:utils.DateUtils.java

License:Open Source License

public static Date findLastMonth() {
    DateTime dt = new DateTime();

    return dt.minusMonths(1).toDate();
}