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;
}
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);
}
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;
}
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);
}
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);
}
Aggregations