Search in sources :

Example 71 with DateTimeUnit

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();
}
Also used : Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) Date(java.util.Date)

Example 72 with DateTimeUnit

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;
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 73 with DateTimeUnit

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);
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 74 with DateTimeUnit

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;
}
Also used : Calendar(org.hisp.dhis.calendar.Calendar) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit)

Example 75 with DateTimeUnit

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);
}
Also used : Matcher(java.util.regex.Matcher) 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