use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.
the class YearlyPeriodType method getRewindedDate.
@Override
public Date getRewindedDate(Date date, Integer rewindedPeriods) {
Calendar calendar = getCalendar();
date = date != null ? date : new Date();
rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(date);
dateTimeUnit = calendar.minusYears(dateTimeUnit, rewindedPeriods);
return calendar.toIso(dateTimeUnit).toJdkDate();
}
use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.
the class BiMonthlyPeriodType method generateRollingPeriods.
/**
* Generates the last 6 bi-months where the last one is the bi-month which
* the given date is inside.
*/
@Override
public List<Period> generateRollingPeriods(DateTimeUnit dateTimeUnit, Calendar calendar) {
dateTimeUnit.setDay(1);
DateTimeUnit iterationDateTimeUnit = calendar.minusMonths(dateTimeUnit, (dateTimeUnit.getMonth() % 2) + 10);
List<Period> periods = Lists.newArrayList();
for (int i = 0; i < 6; i++) {
periods.add(createPeriod(iterationDateTimeUnit, calendar));
iterationDateTimeUnit = calendar.plusMonths(iterationDateTimeUnit, 2);
}
return periods;
}
use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.
the class BiMonthlyPeriodType method createPeriod.
@Override
public Period createPeriod(DateTimeUnit dateTimeUnit, Calendar calendar) {
DateTimeUnit start = new DateTimeUnit(dateTimeUnit);
start.setMonth(((start.getMonth() - 1) - ((start.getMonth() - 1) % 2)) + 1);
start.setDay(1);
DateTimeUnit end = new DateTimeUnit(start);
end = calendar.plusMonths(end, 1);
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 CalendarPeriodType method generateLast5Years.
/**
* Generates a list of Periods for the last 5 years. Must be overridden by
* CalendarPeriodTypes which do not generate periods for the current year
* only in their implementation of generatePeriods( Date ).
*
* @param date the date which touches the time span to generate Periods for.
* @return a list of Periods for a defined time span.
*/
public List<Period> generateLast5Years(Date date) {
DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(date);
dateTimeUnit = getCalendar().minusYears(dateTimeUnit, 4);
List<Period> periods = Lists.newArrayList();
Calendar calendar = getCalendar();
for (int i = 0; i < 5; i++) {
periods.addAll(generatePeriods(dateTimeUnit));
dateTimeUnit = calendar.plusYears(dateTimeUnit, 1);
}
return periods;
}
use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.
the class DateUtils method dateIsValid.
/**
* This method checks whether the String inDate is a valid date following
* the format "yyyy-MM-dd".
*
* @param calendar Calendar to be used
* @param dateString the string to be checked.
* @return true/false depending on whether the string is a date according to
* the format "yyyy-MM-dd".
*/
public static boolean dateIsValid(org.hisp.dhis.calendar.Calendar calendar, String dateString) {
Matcher matcher = DEFAULT_DATE_REGEX_PATTERN.matcher(dateString);
if (!matcher.matches()) {
return false;
}
DateTimeUnit dateTime = new DateTimeUnit(Integer.parseInt(matcher.group("year")), Integer.parseInt(matcher.group("month")), Integer.parseInt(matcher.group("day")));
return calendar.isValid(dateTime);
}
Aggregations