Search in sources :

Example 56 with DateTimeUnit

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

the class WeeklyAbstractPeriodType method generatePeriods.

/**
 * Generates weekly Periods for the whole year in which the given Period's
 * startDate exists.
 */
@Override
public List<Period> generatePeriods(DateTimeUnit start) {
    Calendar calendar = getCalendar();
    List<Period> periods = new ArrayList<>();
    // create clone so we don't modify
    start = new DateTimeUnit(start);
    // the original start DT
    start.setMonth(1);
    start.setDay(4);
    start = adjustToStartOfWeek(start, calendar);
    for (int i = 0; i < calendar.weeksInYear(start.getYear()); i++) {
        DateInterval interval = calendar.toInterval(start, DateIntervalType.ISO8601_WEEK);
        periods.add(new Period(this, interval.getFrom().toJdkDate(), interval.getTo().toJdkDate()));
        start = calendar.plusWeeks(start, 1);
    }
    return periods;
}
Also used : Calendar(org.hisp.dhis.calendar.Calendar) ArrayList(java.util.ArrayList) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) DateInterval(org.hisp.dhis.calendar.DateInterval)

Example 57 with DateTimeUnit

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

the class WeeklyAbstractPeriodType method createPeriod.

@Override
public Period createPeriod(DateTimeUnit dateTimeUnit, Calendar calendar) {
    DateTimeUnit start = adjustToStartOfWeek(new DateTimeUnit(dateTimeUnit), calendar);
    DateTimeUnit end = new DateTimeUnit(start);
    end = calendar.plusDays(end, calendar.daysInWeek() - 1);
    return toIsoPeriod(start, end, calendar);
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 58 with DateTimeUnit

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

the class YearlyPeriodType method generateLast5Years.

/**
 * Generates the last 5 years where the last one is the year which the given
 * date is inside.
 */
@Override
public List<Period> generateLast5Years(Date date) {
    Calendar calendar = getCalendar();
    DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(date);
    dateTimeUnit = calendar.minusYears(dateTimeUnit, 4);
    dateTimeUnit.setDay(1);
    dateTimeUnit.setMonth(1);
    List<Period> periods = Lists.newArrayList();
    for (int i = 0; i < 5; ++i) {
        periods.add(createPeriod(dateTimeUnit, calendar));
        dateTimeUnit = calendar.plusYears(dateTimeUnit, 1);
    }
    return periods;
}
Also used : Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 59 with DateTimeUnit

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

the class BiWeeklyAbstractPeriodType method createPeriod.

@Override
public Period createPeriod(DateTimeUnit dateTimeUnit, Calendar calendar) {
    DateTimeUnit start = adjustToStartOfBiWeek(new DateTimeUnit(dateTimeUnit), calendar);
    DateTimeUnit end = new DateTimeUnit(start);
    end = calendar.plusDays(end, calendar.daysInWeek() * 2 - 1);
    return toIsoPeriod(start, end, calendar);
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 60 with DateTimeUnit

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

the class BiWeeklyAbstractPeriodType method getIsoDate.

@Override
public String getIsoDate(DateTimeUnit dateTimeUnit, Calendar calendar) {
    int year;
    int week;
    if (calendar.isIso8601()) {
        LocalDate date = LocalDate.of(dateTimeUnit.getYear(), dateTimeUnit.getMonth(), dateTimeUnit.getDay());
        WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 4);
        year = date.get(weekFields.weekBasedYear());
        week = (date.get(weekFields.weekOfWeekBasedYear()) / 2) + 1;
    } else {
        DateTimeUnit date = adjustToStartOfBiWeek(dateTimeUnit, calendar);
        week = calendar.week(date);
        if (week == 1 && date.getMonth() == calendar.monthsInYear()) {
            date.setYear(date.getYear() + 1);
        }
        year = date.getYear();
    }
    return String.format("%d%s%d", year, weekPrefix, week);
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate)

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