Example usage for org.joda.time DateMidnight minusYears

List of usage examples for org.joda.time DateMidnight minusYears

Introduction

In this page you can find the example usage for org.joda.time DateMidnight minusYears.

Prototype

public DateMidnight minusYears(int years) 

Source Link

Document

Returns a copy of this date minus the specified number of years.

Usage

From source file:org.apereo.portal.portlets.statistics.BaseStatisticsReportController.java

License:Apache License

/**
 * Set the start/end date and the interval to have selected by default if they are not already
 * set//w w  w  .  j ava2  s .c o  m
 */
protected final void setReportFormDateRangeAndInterval(final F report) {
    //Determine default interval based on the intervals available for this aggregation
    if (report.getInterval() == null) {
        report.setInterval(AggregationInterval.DAY);
        final Set<AggregationInterval> intervals = this.getIntervals();
        for (final AggregationInterval preferredInterval : PREFERRED_INTERVAL_ORDER) {
            if (intervals.contains(preferredInterval)) {
                report.setInterval(preferredInterval);
                break;
            }
        }
    }

    //Set the report end date as today
    final DateMidnight reportEnd;
    if (report.getEnd() == null) {
        reportEnd = new DateMidnight();
        report.setEnd(reportEnd);
    } else {
        reportEnd = report.getEnd();
    }

    //Determine the best start date based on the selected interval
    if (report.getStart() == null) {
        final DateMidnight start;
        switch (report.getInterval()) {
        case MINUTE: {
            start = reportEnd.minusDays(1);
            break;
        }
        case FIVE_MINUTE: {
            start = reportEnd.minusDays(2);
            break;
        }
        case HOUR: {
            start = reportEnd.minusWeeks(1);
            break;
        }
        case DAY: {
            start = reportEnd.minusMonths(1);
            break;
        }
        case WEEK: {
            start = reportEnd.minusMonths(3);
            break;
        }
        case MONTH: {
            start = reportEnd.minusYears(1);
            break;
        }
        case ACADEMIC_TERM: {
            start = reportEnd.minusYears(2);
            break;
        }
        case CALENDAR_QUARTER: {
            start = reportEnd.minusYears(2);
            break;
        }
        case YEAR: {
            start = reportEnd.minusYears(10);
            break;
        }
        default: {
            start = reportEnd.minusWeeks(1);
        }
        }

        report.setStart(start);
    }
}

From source file:se.streamsource.streamflow.client.ui.workspace.table.PerspectivePeriodModel.java

License:Apache License

private String getSearchPeriod(Date fromDate, int direction, String periodName, String datePattern,
        String separator) {/*from  ww  w. j  a  v  a 2s.  co  m*/
    DateMidnight from = new DateMidnight(fromDate);
    DateMidnight to = null;
    DateTimeFormatter format = DateTimeFormat.forPattern(datePattern);

    switch (Period.valueOf(periodName)) {
    case one_day:
        return format.print(from);

    case three_days:
        to = (direction == 1) ? from.plusDays(2) : from.minusDays(2);
        break;

    case one_week:
        to = (direction == 1) ? from.plusWeeks(1).minusDays(1) : from.minusWeeks(1).plusDays(1);
        break;

    case two_weeks:
        to = (direction == 1) ? from.plusWeeks(2).minusDays(1) : from.minusWeeks(2).plusDays(1);
        break;

    case one_month:
        to = (direction == 1) ? from.plusMonths(1).minusDays(1) : from.minusMonths(1).plusDays(1);
        break;

    case six_months:
        to = (direction == 1) ? from.plusMonths(6).minusDays(1) : from.minusMonths(6).plusDays(1);
        break;

    case one_year:
        to = (direction == 1) ? from.plusYears(1).minusDays(1) : from.minusYears(1).plusDays(1);
        break;

    }
    return (direction == 1) ? format.print(from) + separator + format.print(to)
            : format.print(to) + separator + format.print(from);

}