Search in sources :

Example 66 with DateTimeUnit

use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.

the class FinancialPeriodType method generateLast5Years.

@Override
public List<Period> generateLast5Years(Date date) {
    Calendar cal = getCalendar();
    DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(date, cal);
    boolean past = dateTimeUnit.getMonth() >= (getBaseMonth() + 1);
    List<Period> periods = Lists.newArrayList();
    dateTimeUnit = cal.minusYears(dateTimeUnit, past ? 4 : 5);
    dateTimeUnit.setMonth(getBaseMonth() + 1);
    dateTimeUnit.setDay(1);
    for (int i = 0; i < 5; i++) {
        periods.add(createPeriod(dateTimeUnit, cal));
        dateTimeUnit = cal.plusYears(dateTimeUnit, 1);
    }
    return periods;
}
Also used : Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 67 with DateTimeUnit

use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.

the class PeriodType method getDateWithOffset.

/**
 * Offsets the input date with the provided number of periods within the
 * current period type. If the offset number is positive, the date is offset
 * into later periods. When the offset is negative, the date is offset into
 * earlier periods.
 *
 * @param date for where to start the offset.
 * @param offset how many periods to go back(if negative) or forward(if
 *        positive). A value of 0 will result in the original date to be
 *        returned.
 * @return a new date object that has been offset from the original date
 *         passed into the function.
 */
public Date getDateWithOffset(Date date, int offset) {
    org.hisp.dhis.calendar.Calendar calendar = getCalendar();
    DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(date, calendar);
    return getDateWithOffset(dateTimeUnit, offset, calendar).toJdkDate();
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 68 with DateTimeUnit

use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.

the class PeriodType method createPeriod.

// -------------------------------------------------------------------------
// ISO format methods
// -------------------------------------------------------------------------
/**
 * @param dateInterval DateInterval to create period from
 * @return the period.
 */
public Period createPeriod(DateInterval dateInterval) {
    if (dateInterval == null || dateInterval.getFrom() == null || dateInterval.getTo() == null) {
        return null;
    }
    org.hisp.dhis.calendar.Calendar cal = getCalendar();
    final DateTimeUnit from = cal.toIso(dateInterval.getFrom());
    final DateTimeUnit to = cal.toIso(dateInterval.getTo());
    return new Period(this, from.toJdkDate(), to.toJdkDate(), getIsoDate(from));
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 69 with DateTimeUnit

use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.

the class SixMonthlyAbstractPeriodType method createPeriod.

// -------------------------------------------------------------------------
// PeriodType functionality
// -------------------------------------------------------------------------
@Override
public Period createPeriod(DateTimeUnit dateTimeUnit, org.hisp.dhis.calendar.Calendar calendar) {
    DateTimeUnit start = new DateTimeUnit(dateTimeUnit);
    int baseMonth = getBaseMonth();
    int year = start.getMonth() < baseMonth ? (start.getYear() - 1) : start.getYear();
    int month = start.getMonth() >= baseMonth && start.getMonth() < (baseMonth + 6) ? baseMonth : (baseMonth + 6);
    start.setYear(year);
    start.setMonth(month);
    start.setDay(1);
    DateTimeUnit end = new DateTimeUnit(start);
    end = calendar.plusMonths(end, 5);
    end.setDay(calendar.daysInMonth(end.getYear(), end.getMonth()));
    return toIsoPeriod(start, end, calendar);
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 70 with DateTimeUnit

use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.

the class SixMonthlyNovemberPeriodType method createPeriod.

@Override
public Period createPeriod(DateTimeUnit dateTimeUnit, Calendar calendar) {
    DateTimeUnit start = new DateTimeUnit(dateTimeUnit);
    int baseMonth = getBaseMonth();
    int year = start.getYear();
    int month = baseMonth;
    if (start.getMonth() < 5) {
        month = baseMonth;
        year = year - 1;
    }
    if (start.getMonth() >= 5 && start.getMonth() <= 10) {
        month = baseMonth - 6;
    }
    start.setYear(year);
    start.setMonth(month);
    start.setDay(1);
    DateTimeUnit end = new DateTimeUnit(start);
    end = calendar.plusMonths(end, 5);
    end.setDay(calendar.daysInMonth(end.getYear(), end.getMonth()));
    return toIsoPeriod(start, end, calendar);
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Aggregations

DateTimeUnit (org.hisp.dhis.calendar.DateTimeUnit)98 Calendar (org.hisp.dhis.calendar.Calendar)23 Test (org.junit.jupiter.api.Test)17 Date (java.util.Date)15 DateInterval (org.hisp.dhis.calendar.DateInterval)10 Period (org.hisp.dhis.period.Period)6 DateTime (org.joda.time.DateTime)5 LocalDate (java.time.LocalDate)4 DimensionalObject (org.hisp.dhis.common.DimensionalObject)4 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)4 ArrayList (java.util.ArrayList)3 WeekFields (java.time.temporal.WeekFields)2 HashMap (java.util.HashMap)2 Matcher (java.util.regex.Matcher)2 InvalidCalendarParametersException (org.hisp.dhis.calendar.exception.InvalidCalendarParametersException)2 DataDimensionalItemObject (org.hisp.dhis.common.DataDimensionalItemObject)2 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 DateUtils.getMediumDateString (org.hisp.dhis.system.util.DateUtils.getMediumDateString)2 DateUtils.getMediumDateString (org.hisp.dhis.util.DateUtils.getMediumDateString)2