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